HDU 4283 You Are the One 区间dp
来源:互联网 发布:小世界网络的应用 编辑:程序博客网 时间:2024/06/05 07:46
题意:给定n(n<=100)个人,每一个有固定的权值D,现在n个人从1~n排成一排,第i个人第j个出列的时候的unhappiness值是Di* (j-1),现在有一个栈,
可以暂时不让当前的人出列将其压栈,问怎样安排能使得sigma(unhappiness i)最小,求这个最小值。
题解:想dp[i][j]表示[i ,j]内的unhappiness最小值,枚举k(i<=k<j),有两种情况需要讨论:
1 在k时刻[i , k]区间内的人全部在[k+1, j]区间内的人之前出列,且已经全部不在栈中,即[i , j]区间可以分为[i , k] , [k+1 ,j]两个完全相同的子问题,
即dp[i][j] =MIN(dp[i][j] , dp[i][k] + dp[k+1][j] + (sum[j] – sum[i]) * (k – i +1));
2 在k时刻[i , k]区间内的人全部在[k+1 , j]区间内的人之后出列,即[i , k]区间内的人全部需要进栈,所以出来的顺序是逆序的,需O(n2)预处理出against_order[i][j]
表示[i , j]区间人逆序出来的unhappiness值,即dp[i][j] = MIN(dp[i][j] , dp[k+1][j] + against_order[i][k] + (sum[k]– sum[i-1]) * (j - k));
Sure原创,转载请注明出处。
#include <iostream>#include <cstdio>#include <memory.h>#define MIN(a , b) ((a) < (b) ? (a) : (b))using namespace std;const int inf = 1 << 29;const int maxn = 102;int diao[maxn],sum[maxn],against_order[maxn][maxn],dp[maxn][maxn];bool vis[maxn][maxn];int n;void read(){ memset(vis,false,sizeof(vis)); sum[0] = 0; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&diao[i]); sum[i] = sum[i-1] + diao[i]; against_order[i][i] = 0; } return;}void make(){ for(int k=1;k<n;k++) { for(int i=1;i<n;i++) { int j = i + k; if(j > n) break; against_order[i][j] = against_order[i+1][j] + diao[i] * (j - i); } } return;}int dfs(int st,int end){ if(st == end) { vis[st][end] = true; return dp[st][end] = 0; } if(vis[st][end]) { return dp[st][end]; } int res = inf; for(int i=st;i<end;i++) { res = MIN(res , dfs(st , i) + dfs(i+1 , end) + (sum[end] - sum[i]) * (i - st + 1)); res = MIN(res , dfs(i+1 , end) + against_order[st][i] + (sum[i] - sum[st-1]) * (end - i)); } vis[st][end] = true; return dp[st][end] = res;}int main(){ int cas; scanf("%d",&cas); for(int i=1;i<=cas;i++) { read(); make(); printf("Case #%d: %d\n",i,dfs(1,n)); } 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)
- class反编译插件
- 网络基础学习笔记
- WPF中Binding的技巧(一)
- 专题1----从美国印钞看中国当前经济发展形势
- suse12.2硬盘安装的BUG。no repository found
- HDU 4283 You Are the One 区间dp
- EL表达式详解
- (十) 程序文件、预处理指令
- 算法导论中动态规划算法的简单实现
- 【Android API指南】App组件(8) - Services(2) - AIDL
- oracle中的db_file_multiblock_read_count参数
- 每日签到9.10-9.16<坚持的第一周>
- 模板的全特化与偏特化
- 日本将在2013年建立网络防御部队