挑战程序设计竞赛 P131 区间DP

Posted The Azure Arbitrator

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了挑战程序设计竞赛 P131 区间DP相关的知识,希望对你有一定的参考价值。

书上好多题没补
PS.整个DP是根据Q来划分的,dalao的代码就是不一样啊

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define oo 0x3f3f3f3f
using namespace std;
const int maxn = 233;
int A[maxn],P,Q;
int dp[maxn][maxn];
int main(){
    ios::sync_with_stdio(0);
    int T,kase=0; cin>>T;
    while(T--){
        cin>>P>>Q;
        rep(i,1,Q) cin>>A[i];A[Q+1]=P+1;
        memset(dp,0,sizeof dp);
        rep(w,2,Q+1){//width
            rep(i,0,Q+1-w){//st 
                int j=i+w, t=oo;//ed
                rep(k,i+1,j-1){//release
                    t=min(t,dp[i][k]+dp[k][j]);
                }
                dp[i][j]=t+A[j]-A[i]-2;
//              cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
            }
        }
        cout<<"Case #"<<++kase<<": "<<dp[0][Q+1]<<endl;
    }
    return 0;
}

以上是关于挑战程序设计竞赛 P131 区间DP的主要内容,如果未能解决你的问题,请参考以下文章