ZOJ 3703 Happy Programming Contest

Description

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 integersT (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

先按时间排序,再做01背包。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1005;
int n,t,T,f[maxn][4];

struct abc
{
	int t,v;
}a[maxn];

bool cmp(const abc&a,const abc&b)
{
	if (a.t==b.t) return a.v<b.v;
	else return a.t<b.t;
}

int main()
{
	cin>>T;
	while (T--)
	{
		cin>>t>>n;
		for (int i=0;i<n;i++) scanf("%d",&a[i].t);
		for (int i=0;i<n;i++) scanf("%d",&a[i].v);
		sort(a,a+n,cmp);
		for (int i=0;i<=t;i++) f[i][0]=f[i][1]=f[i][2]=f[i][3]=0;
		for (int i=0;i<n;i++)
			for (int j=t;j>=0;j--)
			if (j>=a[i].t)
			{
				if (f[j][0]==f[j-a[i].t][0]+a[i].v) 
				{
					if (f[j][1]==f[j-a[i].t][1]+1)
					{
						int y=f[j-a[i].t][3]+a[i].t;
						if (f[j][2]>f[j-a[i].t][2]+y)
						{
							f[j][3]=f[j-a[i].t][3]+a[i].t;
							f[j][2]=f[j-a[i].t][2]+f[j][3];
						}
					}
					if (f[j][1]<f[j-a[i].t][1]+1)
					{
						f[j][1]=f[j-a[i].t][1]+1;
						f[j][3]=f[j-a[i].t][3]+a[i].t;
						f[j][2]=f[j-a[i].t][2]+f[j][3];
					}
				}
				if (f[j][0]<f[j-a[i].t][0]+a[i].v) 
				{
					f[j][0]=f[j-a[i].t][0]+a[i].v;
					f[j][1]=f[j-a[i].t][1]+1;
					f[j][3]=f[j-a[i].t][3]+a[i].t;
					f[j][2]=f[j-a[i].t][2]+f[j][3];
				}
			}
		printf("%d %d %d\n",f[t][0],f[t][1],f[t][2]);
	}
	return 0;
}


你可能感兴趣的:(ZOJ)