HDU 4283 You Are the One (区间DP,记忆化搜索)
来源:互联网 发布:sas编程与数据挖掘商业案例 编辑:程序博客网 时间:2024/06/14 11:30
题目链接
题目的意思是:
有一个队列,每个人有一个愤怒值D,如果他是第K个上场,不开心指数就为(K-1)*D。但是边上有一个小黑屋(栈),可以一定程度上调整上场程序,使得不开心值最小
学习一下区间dp的问题
dp[i][j]: 表示区间[i,j]的不开心值最小。
当前只考虑这一段区间里面的人的话,也就是这j-i+1个人出栈的名次是[1,j-i+1], 那么假设第i个人是第k个出栈的话,那么[i+1,i+k-1]的人肯定是在第i个人出栈之前就出栈了,然后[i+k,j]的人都是在第i个人出栈之后才出栈的。这两个新的区间就成为了新的子问题。
所以 dp[i][j] = min ( dp[i][j] , dp[i+1][i+k-1] + dp[i+k][j] + (k-1) * a[i] +(k+1-1)*(sum[j] - sum[i+k-1]));
sum是前缀和。因为在第i个人后面的出栈的人名次最少也是偏移k+1次,提前计算着段偏移值,使得后来计算这个区间的时候,仍然可以从一到区间长度的名次出栈,就是原问题的子问题了
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;#define LL long long#define cl(a,b) memset(a,b,sizeof(a))#define gcd __gcdconst int maxn = 105;int a[maxn];int dp[maxn][maxn];int sum[maxn];int dfs(int i,int j){ if(i >= j) return 0; if(dp[i][j] != 0x3f3f3f3f) return dp[i][j]; for(int k = 1; k <= j-i+1; k++){ int left = dfs(i+1, i+k-1); int right = dfs(i+k, j); dp[i][j] = min(dp[i][j], left + right + (k - 1) * a[i] + k * (sum[j] - sum[i+k-1])); } return dp[i][j];}int main(){ int T;int cas = 1; scanf("%d", &T); while(T--){ int n; scanf("%d", &n); sum[0] = 0; for(int i = 1; i <= n; i++){ scanf("%d", &a[i]); sum[i] = sum[i-1] + a[i]; } cl(dp,0x3f); int ans = dfs(1,n); printf("Case #%d: %d\n", cas++, ans); } return 0;}
0 0
- hdu 4283 You Are the One(记忆化搜索,区间dp)
- HDU 4283 You Are the One (区间DP,记忆化搜索)
- hdu4283 You Are the One 区间dp 记忆化搜索or递推
- 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)
- dubbo 异常Please check registry access list (whitelist/blacklist)的分析与解决
- 协方差矩阵
- hdu 1010 Tempter of the Bone (dfs)
- 初步接触三星S健康
- Linux-Shell-awk&指定列去重
- HDU 4283 You Are the One (区间DP,记忆化搜索)
- HDU-5893 树链剖分+区间合并
- UIImage改变图片方向
- 机器人的运动范围
- 关于line-height相关知识(以及字号,基线,行内框)
- HDU 1061 快速幂
- 面向对象
- GCD exGCD 逆元 快速幂 大数取模
- Ubuntu下设置ssh登录