hdu_4283 You Are the One # by nobody
来源:互联网 发布:ubuntu 16.04 云下载 编辑:程序博客网 时间:2024/05/16 06:25
天津网赛一题简单的DP..
题意:给定n(n<=100)个人,每一个有固定的权值D,现在n个人从1~n排成一排,第i个人第j个出列的时候的unhappiness值是Di* (j-1),现在有一个栈,
可以暂时不让当前的人出列将其压栈,问怎样安排能使得sigma(unhappiness i)最小,求这个最小值。
解析:记忆化搜索dp[x][y]代表x-y人排队的最小sigma值,分第x个人第i次出栈得出最优子结构递归。
详见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[110][110];
int s[110];
int d[110];
int dfs(int x,int y)
{
if(dp[x][y]!=-1)return dp[x][y];
if(x>=y)return dp[x][y] = 0;
int temp=1<<30;
for(int i=x;i<=y;i++)
{
temp = min(temp,dfs(x+1,i)+dfs(i+1,y)+(s[y]-s[i])*(i-x+1)+d[x]*(i-x));
}
return dp[x][y] = temp;
}
int main()
{
int cases;
scanf("%d",&cases);
int t = 1;
while(cases--)
{
int n;
memset(dp,-1,sizeof(dp));
scanf("%d",&n);
s[0] = 0;
for(int i=1;i<=n;i++)
{
scanf("%d",&d[i]);
s[i] = s[i-1]+d[i];
}
printf("Case #%d: %d\n",t++,dfs(1,n));
}
return 0;
}
- hdu_4283 You Are the One # by nobody
- You Are the One----HDU_4283----DP
- You Are the One
- hdu4283 You Are the One
- HDU4283 You Are the One
- 【HDU4283】【You Are the One】
- HDU4283-You Are the One
- HDU4283 You Are the One
- You Are the One HDU
- HDU4283---You Are the One
- hdu4283 You Are the One(区间DP)
- HDU4283:You Are the One(区间DP)
- hdu 4283 You Are the One
- HDU 4283 You Are the One
- HDOJ 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
- 字符串专题:C - Inglish-Number (字符串操作基础题)
- TCHAR、WCHAR、CHAR以及WideCharToMultiByte、MultiByteToWideChar
- Response.Write 与 Page.ClientScript.RegisterStartupScript
- matlab内存问题
- IOS-UILabel
- hdu_4283 You Are the One # by nobody
- 就算世间都是寒冬般冰冷,我们也会用掌心的温度温暖彼此。希望就像黑夜里的一丝烛火,永不熄灭。
- 字符串专题:F - Oulipo (A包含多少次B?KMP)
- java的基本网络应用
- iOS-UIButton
- 11 java 王少飞-在Java中实现TCP协议编程
- 回归自然
- 字符串专题:G - Milking Grid (二维kmp)
- Android Training - 网络操作(2) - 管理网络用途