In Zhejiang University Programming Contest, a team is called “couple team” if it consists of only two students loving each other. In the contest, the team will get a lovely balloon with unique color for each problem they solved. Since the girl would prefer pink balloon rather than black balloon, each color is assigned a value to measure its attractiveness. Usually, the boy is good at programming while the girl is charming. The boy wishes to solve problems as many as possible. However, the girl cares more about the lovely balloons. Of course, the boy’s primary goal is to make the girl happy rather than win a prize in the contest.
Suppose for each problem, the boy already knows how much time he needs to solve it. Please help him make a plan to solve these problems in strategic order so that he can maximize the total attractiveness value of balloons they get before the contest ends. Under this condition, he wants to solve problems as many as possible. If there are many ways to achieve this goal, he needs to minimize the total penalty time. The penalty time of a problem is equal to the submission time of the correct solution. We assume that the boy is so clever that he always submit the correct solution.
Input
The first line of input is an integer N (N < 50) indicating the number of test cases. For each case, first there is a line containing 2 integers T (T <= 1000) and n (n <= 50) indicating the contest length and the number of problems. The next line contains n integers and the i-th integer ti (ti <= 1000) represents the time needed to solve the ith problem. Finally, there is another line containing n integers and the i-th integer vi (vi <= 1000) represents the attractiveness value of the i-th problem. Time is measured in minutes.
Output
For each case, output a single line containing 3 integers in this order: the total attractiveness value, the number of problems solved, the total penalty time. The 3 integers should be separated by a space.
Sample Input
2
300 10
10 10 10 10 10 10 10 10 10 10
1 2 3 4 5 6 7 8 9 10
300 10
301 301 301 301 301 301 301 301 301 301
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
Sample Output
55 10 550
0 0 0
题目链接
参考题解
- 题目讲的是一对情侣在赛码场上,每道题对应不同颜色的气球,不同的气球对妹子的吸引力不同。在已知程序员做每道题所需的时间、比赛总时长以及每一个气球对妹子的吸引值。求在能力和时间允许的前提下,得到的气球对妹子吸引力的总值最大为多少。要求输出,吸引总值,解题数目,罚时。
- 这个题目在做的时候,感觉跟背包很像,但是由于状态量很多,不知道应该怎么记录。赛后看了看题解,可以将遍历过程中的每一个状态用数组记录下来。开一个二维数组,第一维代表题目序号,第二维表示做完这个题目为止已经用了多少时间。数组内部存储0或1,代表有没有经历这个状态。这样,我们从后面往前遍历,也就是从最后一个题目往前遍历,只要这个状态经历过,那么就把这个题目放上,说明做完了,那么到最后取完一定能保证做的题目个数最多。然后再按照每道题目的花费时间排序,这样就能保证罚时最短了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e3 + 10;
int f[maxn], G[55][maxn], t[55], v[55], ord[55];
int n, tt;
int main()
{
int T;
cin >> T;
while(T--)
{
scanf("%d%d", &tt, &n);
for(int i = 1; i <= n; i++) scanf("%d", &t[i]);
for(int i = 1; i <= n; i++) scanf("%d", &v[i]);
memset(f, 0, sizeof(f));
memset(G, 0, sizeof(G));
for(int i = 1; i <= n; i++)
{
for(int j = tt; j >= t[i]; j--)
{
if(f[j] < f[j - t[i]] + v[i])
{
f[j] = f[j - t[i]] + v[i];
G[i][j] = 1;
}
}
}
int cnt = 0, i = n, j = tt;
while(i)
{
if(G[i][j] == 1)
{
ord[cnt++] = t[i];
j -= t[i];
}
i--;
}
int pently = 0, sum = 0;
sort(ord, ord + cnt);
for(int i = 0; i < cnt; i ++)
{
sum += ord[i];
pently += sum;
}
printf("%d %d %d\n", f[tt], cnt, pently);
}
return 0;
}
本文探讨了一个情侣参加编程竞赛的问题,男方擅长编程,女方注重气球的吸引力。通过动态规划算法,确定了在有限时间内,如何选择解决问题以最大化女方对气球的满意度,同时考虑了解决问题的数量和罚时。
1606

被折叠的 条评论
为什么被折叠?



