HDU4283——You Are the One (区间DP)
来源:互联网 发布:电气原理图制图软件 编辑:程序博客网 时间:2024/06/05 06:43
有n个男屌丝事先按1,2,3,,,,,,n的顺序排好,
每个人都有一个不开心值unhappy[i],如果第i个人
第k个上台找对象,那么该屌丝男的不开心值就会为
(k-1)*unhappy[i],因为在他前面有k-1个人嘛,导演
为了让所有男屌的总不开心值最小,搞了一个小黑屋,
可以通过小黑屋来改变男屌的出场顺序
注意 :这个小黑屋是个栈,男屌的顺序是排好了的,但是可以通过
入栈出栈来改变男屌的出场顺序
队列有一个性质就是如果i第k个出栈那么 i+k-1之后的都必须在k之后出栈
举个例子吧:
有5个人事先排好顺序 1,2,3,4,5
入栈的时候,1入完2入,2入完3入,如果我要第1个人第3个出场,那么入栈出栈顺序是这样的:
1入,2入,3入,3出,2出,1出(到此第一个人就是第3个出场啦,很明显第2,3号人
要在1先出,而4,5要在1后出)
这样子, 动态转移方程 就出来啦,根据第i个人是第k个出场的,将区间[i,j]分成3个部分
dp[i][j]=min(dp[i][j],dp[i+1,i+k-1]+dp[i+k,j]+(k-1)*a[i]+(sum[j]-sum[i+k-1])*k);
(sum[j]-sum[i+k-1])*k 表示 后面的 j-i-k+1个人是在i后面才出场的,那么每个人的不开心
值都会加个 unhappy,sum[i]用来记录前面i个人的总不开心值,
根据题目,每个人的unhappy是个 累加的过程 ,多等一个人,就多累加一次
dp这个是用来求最低值的,慢慢来,慢慢理解多做几道
小结:区间dp就是2——n个区间,每个区间求小值然后在一步步到大区间最后就求出结果,
建议新手去看下石子划分
坑爹的dp,屌丝的dp。如果能重来,我要在A一次dp
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define inf 0x3fffffffusing namespace std;int dp[105][105];int val[105],sum[105];int main(){ int t,iCase=1,n;scanf("%d",&t);while(t--){sum[0]=0;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&val[i]);sum[i]=sum[i-1]+val[i];}memset(dp,0,sizeof dp);for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){dp[i][j]=inf;}}for(int p=2;p<=n;p++) { for(int i=1;i+p<=n+1;i++) { int j=i+p-1; for(int k=1;k<=p;k++) { dp[i][j]=min(dp[i][j],dp[i+1][k+i-1]+dp[k+i][j]+(k-1)*val[i]+k*(sum[j]-sum[i+k-1])); } } }printf("Case #%d: %d\n",iCase++,dp[1][n]);}return 0;}
0 0
- HDU4283——You Are the One(区间DP)
- hdu4283——You Are the One(区间dp)
- HDU4283——You Are the One(区间dp)
- HDU4283——You Are the One (区间DP)
- hdu4283 You Are the One(区间DP)
- HDU4283:You Are the One(区间DP)
- hdu4283---You Are the One(区间dp)
- hdu4283 you are the one 区间DP
- HDU4283:You Are the One(区间DP)
- HDU4283 You Are the One 区间DP
- hdu4283 You Are the One(区间DP)
- hdu4283 You Are the One 区间DP
- HDU4283:You Are the One(区间DP)
- hdu4283 You Are the One(区间DP)
- hdu4283 You Are the One(区间dp,思考)
- HDU4283 You Are the One(区间dp)
- HDU4283 You Are the One (DP)
- HDU4283 You Are the One 有点难的区间DP
- LINUX进程相关的几个函数
- hdu 2824 The Euler function
- hdu4405_Aeroplane chess
- Eclipse如何输入一个或几个字母后,自动联想后边的字母
- 利用RxJava获取手机已安装的App的图片、应用名称和版本号
- HDU4283——You Are the One (区间DP)
- Python异常处理 -跳过异常继续执行
- MYSQL的COMMIT和ROLLBACK
- html5的一些新特性
- Python-【基础】-Windows平台搭建一个完整的IDE环境
- 练习shell脚本
- QTableWidget详解(样式、右键菜单、表头塌陷、多选等)
- 如何理解补码这一概念
- C++比较符重载