牛客网暑期ACM多校训练营(第三场)PACM Team

链接:https://www.nowcoder.com/acm/contest/141/A
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld

题目描述

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.

示例1

输入

复制

2
1 0 2 1 10
1 0 2 1 21
1 0 2 1

输出

复制

1
1

示例2

输入

复制

1
2 1 1 0 31
1 0 2 1

输出

复制

0

题意:这有N个团队,第i个团队有pi,ai,ci,mi,gi 五个属性值,要求邀请的队伍不超过P,A,C,M这四个值,并且要求邀请的g值和越高越好;问最多邀请多少个队,并且输出每个队的编号;

思路:四维01背包,他的编号就用另一个五维的数组记录路径就好了,当然要开bool或者short否则可能会爆;

下面附上我的代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define lson o<<1
#define rson o<<1|1
using namespace std;
int dp[40][40][40][40];
bool path[40][40][40][40][40]; 
int P, A, C , M, N;
struct node{
	int p, a, c, m, g;
}s[40]; 
int main()
{
	int N;
	cin >> N;
	for(int i = 1; i <= N; i++)
		scanf("%d %d %d %d %d", &s[i].p, &s[i].a, &s[i].c, &s[i].m, &s[i].g);
	cin >> P >> A >> C >> M;
	for(int i = 1; i <= N; i++)
		for(int j = P; j >= s[i].p; j--)
			for(int k = A; k >= s[i].a; k--)
				for(int q = C; q >= s[i].c; q--)
					for(int e = M; e >= s[i].m; e--)
					{
						if(dp[j][k][q][e] < dp[j - s[i].p][k - s[i].a][q - s[i].c][e - s[i].m] + s[i].g)
						{
							dp[j][k][q][e] = dp[j - s[i].p][k - s[i].a][q - s[i].c][e - s[i].m] + s[i].g;
							path[i][j][k][q][e] = true;
						}
					}
	stack<int> ans;
	for(int i = N, j = P, k = A, q = C, e = M; i >= 1&&j >= 0&&k >= 0&&q >= 0&&e >= 0; i--)
	{
		if(path[i][j][k][q][e])
		{
			j -= s[i].p;
			k -= s[i].a;
			q -= s[i].c;
			e -= s[i].m;
			ans.push(i); 
		}
	}
	cout << ans.size() << endl;
	while(!ans.empty())
	{
		printf("%d\n", ans.top() - 1);
		ans.pop();
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/gtuif/article/details/81540142