HDU --4283--You Are the One--区间DP
来源:互联网 发布:mac播放wav格式 编辑:程序博客网 时间:2024/05/22 19:23
想好出栈入栈的情况,别多想,别少想,然后就可以DP了
假设一段区间[s,e],中间点是m,那么m把区间分成了两部分,[s,m]和[m+1,e],设dp[i][j]为区间[i,j]最小吊丝和,sum[i][j]为区间[i,j]的每个男生的吊丝值的和.
那么,[s,m],和[m+1,e]两段区间就可以有两个决策:
一:区间一的所有人先上舞台。
二:区间二的所有人先上舞台。
如果区间二的人先上舞台的话,那么区间一的所有人都必须按照先后顺序依次进栈,所以设一个数组diaosi[i][j]表示区间[i,j]的所有人从左到右依次进栈,然后出栈的吊丝和。
然后状态转移方程请看代码~~
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cstdlib>#include<map>#include<queue>#include<vector>using namespace std;const int MAXN = 120;const int INF = 999999999;int dp[MAXN][MAXN];int a[MAXN],sum[MAXN][MAXN],n;int diaosi[MAXN][MAXN];void OP_DP(){ for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) printf("dp[%d][%d] -> %d\n",i,j,dp[i][j]); }}void make_diaosi(){ memset(diaosi,0,sizeof(diaosi)); for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { for(int k=i;k<j;k++) { diaosi[i][j]+=sum[i][k]; } //printf("ds[%d][%d] -> %d\n",i,j,diaosi[i][j]); } }}void solve(){ for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) { sum[i][j]=a[j]-a[i-1]; dp[i][j]=INF; } make_diaosi(); for(int i=1;i<=n;i++) { dp[i][i]=0; } for(int k=1;k<=n;k++) { for(int i=1;i+k<=n;i++) { for(int j=i;j<i+k;j++) { dp[i][i+k]=min(dp[i][i+k],dp[i][j]+sum[j+1][i+k]*(j-i+1)+dp[j+1][i+k]); dp[i][i+k]=min(dp[i][i+k],diaosi[i][j]+sum[i][j]*(i+k-j)+dp[j+1][i+k]); } } } printf("%d\n",dp[1][n]);}int readInt(){ char c; int res=0; while(1) { c=getchar(); if(c>='0'&&c<='9') { res=c-'0'; while(1) { c=getchar(); if(c>='0'&&c<='9') { res=res*10+c-'0'; } else { return res; } } } }}int main(){ //freopen("youare.in","r",stdin); //freopen("me.txt","w",stdout); //freopen("you.txt","r",stdin); int T; T=readInt(); a[0]=0; for(int i=1;i<=T;i++) { n=readInt(); for(int j=1;j<=n;j++) { a[j]=readInt(); a[j]+=a[j-1]; } printf("Case #%d: ",i); solve(); //OP_DP(); } return 0;}
- HDU 4283 You Are the One 区间dp
- hdu 4283 You Are the One 区间dp
- HDU 4283 You Are the One(区间DP)
- hdu 4283 You Are the One(区间dp)
- HDU --4283--You Are the One--区间DP
- hdu 4283 You Are the One(区间DP)
- HDU 4283 You Are the One(区间dp)
- HDU 4283 You Are the One 区间dp
- HDU 4283 You Are the One (区间DP)
- HDU 4283 You Are the One(区间dp)
- hdu 4283 You Are the One(区间dp)
- Hdu 4283 You Are the One(区间dp)
- HDU 4283 You are the one(区间DP)
- HDU 4283 You Are the One(区间DP)
- HDU 4283 You Are the One (区间dp)
- HDU 4283 You Are the One(区间dp)
- hdu 4283 You are the one 区间dp
- HDU 4283 You Are the One (区间DP)
- 鼠标悬浮图片显示原图 移出鼠标后原图消失 js
- cocos2d-x v3.0新特性及使用
- POJ 题目1088 滑雪(记忆搜索)
- flex中使用css样式修改TextArea滚动条的皮肤
- lowlevel_init.S 分析
- HDU --4283--You Are the One--区间DP
- mysql 存储过程示例1
- 765554368578
- 类的设计原则--OOD
- OpenCv函数cvCloneImage内存泄漏问题
- 处理Blob类型数据,例如将对象保存到blob字段中
- centos 6.3用yum安装中文输入法
- myeclipse怎么把整个项目导入进去?
- Linux线程同步之读写锁(rwlock)