【牛客ACM暑期多校】PACM Team(四维01背包+记录路径)

题目描述

Eddy was a contestant participating in ACM ICPC contests. ACM is short for Algorithm, Coding, Math. Since in the ACM contest, the most important knowledge is about algorithm, followed by coding(implementation ability), then math. However, in the ACM ICPC World Finals 2018, Eddy failed to solve a physics equation, which pushed him away from a potential medal.

Since then on, Eddy found that physics is actually the most important thing in the contest. Thus, he wants to form a team to guide the following contestants to conquer the PACM contests(PACM is short for Physics, Algorithm, Coding, Math).

There are N candidate groups each composed of pi physics experts, ai algorithm experts, ci coding experts, mi math experts. For each group, Eddy can either invite all of them or none of them. If i-th team is invited, they will bring gi knowledge points which is calculated by Eddy's magic formula. Eddy believes that the higher the total knowledge points is, the better a team could place in a contest. But, Eddy doesn't want too many experts in the same area in the invited groups. Thus, the number of invited physics experts should not exceed P, and A for algorithm experts, C for coding experts, M for math experts.

Eddy is still busy in studying Physics. You come to help him to figure out which groups should be invited such that they doesn't exceed the constraint and will bring the most knowledge points in total.

输入描述

The first line contains a positive integer N indicating the number of candidate groups.
Each of following N lines contains five space-separated integer pi, ai, ci, mi, gi indicating that i-th team consists of pi physics experts, ai algorithm experts, ci coding experts, mi math experts, and will bring gi knowledge points.
The last line contains four space-separated integer P, A, C, M indicating the maximum possible number of physics experts, algorithm experts, coding experts, and math experts, respectively.

 1 ≤ N ≤ 36
 0 ≤ pi,ai,ci,mi,gi ≤ 36
 0 ≤ P, A, C, M ≤ 36

输出描述

The first line should contain a non-negative integer K indicating the number of invited groups.
The second line should contain K space-separated integer indicating the index of invited groups(groups are indexed from 0).

You can output index in any order as long as each index appears at most once. If there are multiple way to reach the most total knowledge points, you can output any one of them. If none of the groups will be invited, you could either output one line or output a blank line in the second line.

分析

此题可以简化为一个需记录路径的四维01背包问题。背包和物品有4个属性:P,A,C,M。给定背包4个属性的最大容量,问如何放物品,能使得背包的价值最大。

解决本题之前,先把问题简化,分析如下几个问题:

铺垫1(一维01背包的一维数组形式)

01背包可以由二维数组轻松实现,通过滚动数组的技术可以减少为2个一维数组,但是如何缩减至一个一维数组呢?

如何理解二维降一维呢?对于外层循环中的每一个i值,其实都是不需要记录的,在第i次循环时,所有的dp[0…v]都还未更新时,dp[j]还记录着前i-1个物品在容量为j时的最大价值,这样就相当于还记录着dp[i-1][j]和dp[i-1][j-vol[i]]+val[i]。

为什么要从v开始递减遍历?我举个例子,假设一个物品GG价值1000,体积为2,那么假设我们按【0…..v】这个顺序遍历,那么在j=2时,dp[2] = max(dp[2], dp[0]+1000),那么dp[2] = 1000,当j=4时,dp[4]=max(dp[4], dp[2]+1000), dp[4] = 2000,这时我们再思考一下,GG将被放进背包两次!!,如果我们逆序遍历,就可以避免这种结果。

此外,这里可以进行一个常数优化,将j>=val[i]写进for循环中。因为如果j<Wi时,dp[i][j]=dp[i-1][j]。

for i from 1 to n
    dp[i] = 0

for i from 1 to n
    for j from W down to Wi
        dp[j] = max(dp[j], dp[j-Wi]+Vi)

铺垫2(一维01背包问题记录路径)

方法1:利用状态转移方程逆推

方法2:开一个bool类型的path数组构成了一个01矩阵,path[i][j]==1表示取第i件物品,反之表示不取。

从path[n][w]开始,若path[n][w]==1,则第i件取,转至path[i-1][w-wi];若不取,转至path[i-1][w]...

铺垫3(四维01背包记录路径)

与一维01背包问题基本相似,求解最大值时,只不过循环的嵌套层数不一样。记录路径主要采用方法2,如果第i个取,则转至path[i-1][a-ai][b-bi][c-ci][d-di]即可。

//01背包的标准样式
for(int i=0; i<N; ++i)
	for(int j=P; j >= p[i]; --j)
		for(int k=A; k >= a[i]; --k)
			for(int l=C; l >= c[i]; --l)
				for(int o=M; o >= m[i]; --o)
				{
					if(dp[j][k][l][o] < dp[j-p[i]][k-a[i]][l-c[i]][o-m[i]] + g[i])
					{
						dp[j][k][l][o] = dp[j-p[i]][k-a[i]][l-c[i]][o-m[i]] + g[i];
						path[i][j][k][l][o] = true;
					}
				}

//从最后的情况向前找就可以了
for(int i=N-1,j=P,k=A,l=C,o=M; i>=0&&j>=0&&k>=0&&l>=0&&o>=0; --i)
{
	if(path[i][j][k][l][o]) //表示这个物品被使用了。
	{
		vec.push_back(i);
		j -= p[i];
		k -= a[i];
		l -= c[i];
		o -= m[i];
	}
}

猜你喜欢

转载自blog.csdn.net/leelitian3/article/details/81274105