HDU 4283 You Are the One
来源:互联网 发布:java poi 自动列宽 编辑:程序博客网 时间:2024/06/05 06:09
区间dp,如果一个人入栈的话,那到这个人出栈的那一段上台的人肯定是原本位置和当前位置之间的那些人,所以这一个区间可以考虑为一种情况,所以就把问题弄成把一个大区间分解,找出满足题意的最小值了,于是就成为了区间dp问题了dp[l][r]表示只有这个区间的值时可以得到的最小值,考虑区间(l,r) 则一种情况是把区间继续往下分,于是dp[l,r] = min(dp[l,r], dp[l][i] + dp[i + 1, r] + (sum[r] - sum[i]) * (i - l + 1));或则把 l 到 i 的人放入栈中,由于l必须最后出栈构成当前考虑区间的最后一个,于是考虑是把 l 到 i 的人全放入栈中倒着输出,再加上剩下区间的情况,于是就可以求出ans 了,可以用递推做,也可以用记忆化搜索,个人比较喜欢记忆化,思路清晰些~~~
#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<cstdio>#include<cmath>#define INF 1e9#define LL long long#define PB(a) push_back(a);#define CLR(a, b) memset(a, b, sizeof(a))using namespace std;const int N = 111;int dp[N][N], sum[N], a[N];int dfs(int l, int r){ int i, tmp; if(dp[l][r] != -1) return dp[l][r]; dp[l][r] = INF; for(i = l; i < r; i ++) { dp[l][r] = min(dp[l][r], dfs(l, i) + dfs(i + 1, r) + (sum[r] - sum[i]) * (i - l + 1)); } tmp = 0; for(i = l; i < r; i ++) { tmp += (r - i) * a[i]; dp[l][r] = min(dp[l][r], dfs(i + 1, r) + tmp); } return dp[l][r];}int main(){ int n, t, i, j, cas = 1; scanf("%d", &t); while(t --) { scanf("%d", &n); sum[0] = 0; CLR(dp, -1); for(i = 1; i <= n; i ++) { scanf("%d", &a[i]); sum[i] = sum[i - 1] + a[i]; dp[i][i] = 0; } printf("Case #%d: %d\n", cas ++, dfs(1, n)); }}
- hdu 4283 You Are the One
- HDU 4283 You Are the One
- HDU 4283 You Are the One
- You Are the One - HDU 4283 dp
- You Are the One - HDU 4283 dp
- hdu 4283 You Are the One
- HDU 4283 You Are the One(★)
- hdu 4283 You Are the One
- hdu 4283 You Are the One
- hdu 4283 You Are the One
- hdu 4283 You Are the One
- hdu 4283 You Are the One
- hdu 4283 You Are the One
- You Are the One(HDU 4283)
- You Are the One HDU
- HDU 4283 You Are the One 区间dp
- hdu 4283 You Are the One 区间dp
- HDU 4283 You Are the One(区间DP)
- 学python的一个目标
- zoj2750.。。有关做短路的问题!!
- linux下CVS安装配置
- linux下使用flash magic
- HTML语言中的常用标签
- HDU 4283 You Are the One
- 函数传参的一个问题
- Android开发入门教程--2.Android应用程序结构分析
- Maven知识点记录 - profile
- POJ 1979
- define宏定义和const常量定义之间的区别
- Spring IoC
- install parse failed no certificates
- 聊聊并发(六)——ConcurrentLinkedQueue的实现原理分析