hdoj 4283 You Are the One

原题:
You Are the One
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7879 Accepted Submission(s): 4002

Problem Description
  The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there are n boys enrolling in. At the beginning, the n boys stand in a row and go to the stage one by one. However, the director suddenly knows that very boy has a value of diaosi D, if the boy is k-th one go to the stage, the unhappiness of him will be (k-1)*D, because he has to wait for (k-1) people. Luckily, there is a dark room in the Small hall, so the director can put the boy into the dark room temporarily and let the boys behind his go to stage before him. For the dark room is very narrow, the boy who first get into dark room has to leave last. The director wants to change the order of boys by the dark room, so the summary of unhappiness will be least. Can you help him?

Input
  The first line contains a single integer T, the number of test cases. For each case, the first line is n (0 < n <= 100)
  The next n line are n integer D1-Dn means the value of diaosi of boys (0 <= Di <= 100)

Output
  For each test case, output the least summary of unhappiness .

Sample Input

2
  
5
1
2
3
4
5

5
5
4
3
2
2

Sample Output

Case #1: 20
Case #2: 24

Source
2012 ACM/ICPC Asia Regional Tianjin Online

中文:

n个人参加一个舞台秀,每个人有个属性值 a i a_i ai,如果第i个人在排在第m个上台演出,那么获得分数为 a i ∗ ( m − 1 ) a_i * (m-1) ai(m1)
。此外,给你一个栈,轮到第i个人上台时可以选择入栈,也可以直接登台,也可以从栈顶中弹出一个之前的选手上台,最后求所有人上台分数总和的最小。

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 100 + 5;
typedef long long ll;

int n,t;
int sum[maxn], dp[maxn][maxn],a[maxn];

int main()
{
     
	ios::sync_with_stdio(false);
	cin >> t;
	int cas = 0;
	while (t--)
	{
     
		cin >> n;
		memset(sum, 0, sizeof(sum));
		for (int i = 1; i <= n; i++)
		{
     
			cin >> a[i];
			sum[i] = a[i] + sum[i - 1];
		}
		memset(dp, 0, sizeof(dp));
		for (int len = 2; len <= n; len++)// len = 2
		{
     
			for (int i = 1; i <= n - len + 1; i++) // [1,3]
			{
     
				int j = i + len - 1;// [2,4]
				dp[i][j] = dp[i][j - 1] + (j - i) * a[j];
				for (int k = i; k < j; k++)// [1,2)
				{
     
					//if (k + 1 <= j)
					{
     // i =1 , j =2
						dp[i][j] = min(dp[i][j], dp[i][k - 1] + dp[k + 1][j] + (sum[j] - sum[k]) * (k - 1 - i + 1) + a[k] * (j - i));
					}
				}
			}
		}
		/*
		for (int i = 1; i <= n; i++)
		{
			for (int j = i; j <= n; j++)
			{
				cout << "( " << i << "," << j << ") = " << dp[i][j]<<" ";
			}
			cout << endl;
		}
		*/
		cout << "Case #" << ++cas << ": " << dp[1][n] << endl;
	}
	return 0;
}

/*
1
4
4 2 5 1
*/

解答:

首先,要判断出这题是区间类型的dp。

找个数据在纸上模拟一下,发现n个人中,取其中的一段如第i到第j个人,会呈现下面的几种状态,如图。hdoj 4283 You Are the One_第1张图片

可以看到,第i到第j个人这段区间当中,存在两种状态的段,分别是在栈里的状态段,和已经登台的状态段。如果如过考虑j之后的选手j+1,会有三种选择。第一种是让之前在栈里的人出来登台,第二种是让第j+1个人进栈,第三种是直接让第j+1个人登台。如果选择第j+1个人进栈或者选择让栈里的人直接登台,那么就需要考虑到j个人前面所有在栈里的人的状态,这不现实。由此,这里需要联想到设置这样一个条件,设置 d p [ i ] [ j ] dp[i][j] dp[i][j]表示区间i到j所有人都登台得到的最小分数总和。由此,该问题就引申到区间dp上了。

现在设置好了状态,需要考虑状态转移。首先要知道,题目给定存储选手的数据结构是一个栈,而不是一个队列或者其它什么玩意,在状态转移的时候需要体现出后进先出的意思。

先考虑第j个人做决策,可以第j个人直接登台,那么可以有下面的方程:
d p [ i ] [ j ] = d p [ i ] [ j − 1 ] + a [ j ] ∗ ( j − i ) dp[i][j] = dp[i][j-1] + a[j] * (j -i) dp[i][j]=dp[i][j1]+a[j](ji)

由于第j个人属于在区间i到j中最后一个登台的后面的,所以分数要加上 a [ j ] ∗ ( j − i ) a[j] * (j - i) a[j](ji)

此外,第j个做决策时不能选择从栈中弹出,因为j是区间i到j的最后一个人,由于上台是顺序的,且 d [ i ] [ j ] d[i][j] d[i][j]表示的是区间i到j所有人都登台,所以第j个状态时没法从栈弹出,只能选择上台。

考虑j前面的决策,如果是j之前的人,如第k个人,选择方式就有两种,将第k个人从栈弹出登台,或者第k人直接登台。
如果第k个人从栈中弹出登台,那么状态转移方程需要写成如下形式:
d p [ i ] [ j ] = d p [ i ] [ k − 1 ] + d p [ k + 1 ] [ j ] + ( s u m [ j ] − s u m [ k ] ) ∗ ( k − 1 − i + 1 ) + a [ k ] ∗ ( j − i ) dp[i][j] = dp[i][k-1] + dp[k+1][j] + (sum[j] - sum[k]) * (k - 1 - i + 1) + a[k] * (j - i) dp[i][j]=dp[i][k1]+dp[k+1][j]+(sum[j]sum[k])(k1i+1)+a[k](ji)
解释一下上面的状态转移方程,其中 s u m [ i ] sum[i] sum[i]表示 a [ i ] a[i] a[i]的前缀和,最后的 a [ k ] ∗ ( j − i ) a[k] * (j-i) a[k](ji)含义在上面解释过,而
( s u m [ j ] − s u m [ k ] ) ∗ ( k − 1 − i + 1 ) (sum[j] - sum[k]) * (k - 1 - i + 1) (sum[j]sum[k])(k1i+1) 表示在 d p [ k + 1 ] [ j ] dp[k+1][j] dp[k+1][j]得到的最小值补充上k - 1 - i + 1 倍的a[j]到a[k+1]的和,表示 d p [ i ] [ k − 1 ] dp[i][k-1] dp[i][k1]这个状态先登台 d p [ k + 1 ] [ j ] dp[k+1][j] dp[k+1][j]后登台的补充值。

如果第k个人不从栈中弹出,而是直接登台,那么该状态可以包含在i到j的区间里,在栈中弹出其它值时就已经包含。

考虑,上面的决策状态在进行状态转移的过程中是否满足栈的先进后出?

满足,将上面的状态转移方程中的 d p [ i ] [ k − 1 ] dp[i][k-1] dp[i][k1]或者 d p [ k + 1 ] [ j ] dp[k+1][j] dp[k+1][j]状态继续展开,可得到状态a[k]满足出栈的逻辑。

你可能感兴趣的:(动态规划)