NowCoder HNNU L

https://www.nowcoder.com/acm/contest/127/L

A problem of Interval DP

这题我是一血哦

这题其实我的思路也不是太清晰

想了一个二维DP

但是区间决策的时候瞎搞了搞

然后wa了几次

之后结合了一下

过了

我看别人写的都是三维DP

窃窃自喜

但是可能是我幸运吧

让我二维DP过了

我这么DP对不对我也不知道==

DP这个东西有时候我没法判断自己想的是否正确

不过能写出一道别人很少过的题

还是比较高兴的

虽然是大神都有事

这比赛都没人打(大雾)

Code of AC:

#include<bits/stdc++.h>
using namespace std;
const int N=110;
int A[N],DP[N][N];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;++i) cin>>A[i];
    for(int i=1;i<=n;++i) DP[i][i]=1;
    for(int i=1;i<n;++i){
        for(int j=1;j+i<=n;++j){
            DP[j][i+j]=1<<30;
            if(i<m&&A[j]==A[i+j]){
                int ans=1;
                for(int k=j+1;k<i+j;++k){
                    if(A[k]!=A[j]){
                    	int q;
                        for(q=k;q<i+j;++q){
                            if(A[q]==A[j]){
                                break;
                            }
                        }
                        ans+=DP[k][q-1];
                        k=q-1;
                    }
                }
                DP[j][i+j]=ans;
            }
            if(i<m&&A[j]==A[i+j]){
                int a=j,b=i+j;
                while(A[a]==A[j]&&a<=i+j) ++a;
                while(A[b]==A[j]&&b>=j) --b;
                DP[j][i+j]=min(DP[j][i+j],1+DP[a][b]);
            }
            for(int k=j;k<i+j;++k){
                DP[j][i+j]=min(DP[j][i+j],DP[j][k]+DP[k+1][i+j]);
            }
        }
    }
    cout<<DP[1][n]<<endl;
}

猜你喜欢

转载自blog.csdn.net/gipsy_danger/article/details/80551923
l