首页 > 其他 > 详细

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

时间:2018-01-16 12:48:43      阅读:240      评论:0      收藏:0      [点我收藏+]

书上好多题没补
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

原文:https://www.cnblogs.com/caturra/p/8295954.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!