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
对于两道用时分别为6和4的题,先A4再A6罚时14,先A6再A4罚时16。为保证先A水题,要将时间从小到大排序。
--------------
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int T,m,n,t; struct PRB{ int w; int v; }a[111]; int f[1111]; struct SPS { int ac; int time; } p[1111]; bool cmp(PRB a,PRB b) { return a.w<b.w; } int main() { cin>>T; while (T--) { memset(a,0,sizeof(a)); memset(f,-1,sizeof(f)); memset(p,0,sizeof(p)); cin>>m>>n; for (int i=1; i<=n; i++) { cin>>a[i].w; } for (int i=1; i<=n; i++) { cin>>a[i].v; } sort(a+1,a+1+n,cmp); f[0]=0; for (int i=1; i<=n; i++) { for (int j=m; j>=a[i].w; j--) { if ( f[j-a[i].w]!=-1 ) { if ( f[j-a[i].w]+a[i].v>f[j] ) { f[j]=f[j-a[i].w]+a[i].v; p[j].ac=p[j-a[i].w].ac+1; p[j].time=p[j-a[i].w].time+j; } else if ( f[j-a[i].w]+a[i].v==f[j] ) { if ( p[j-a[i].w].ac+1>p[j].ac ) { f[j]=f[j-a[i].w]+a[i].v; p[j].ac=p[j-a[i].w].ac+1; p[j].time=p[j-a[i].w].time+j; } else if (p[j-a[i].w].ac+1==p[j].ac && p[j-a[i].w].time+j<p[j].time ) { f[j]=f[j-a[i].w]+a[i].v; p[j].ac=p[j-a[i].w].ac+1; p[j].time=p[j-a[i].w].time+j; } } } } } int af=0,aac=0,atime=0; for (int i=m; i>=0; i--) { if (f[i]>af) { af=f[i]; aac=p[i].ac; atime=p[i].time; } else if (f[i]==af) { if (p[i].ac>aac) { aac=p[i].ac; atime=p[i].time; } else if (p[i].ac==aac&&p[i].time<atime) { aac=p[i].ac; atime=p[i].time; } } } /* cerr<<"----"<<endl; for (int i=0;i<=m;i++) { cerr<<f[i]<<" "<<p[i].ac<<" "<<p[i].time<<endl; } cerr<<"----"<<endl; */ cout<<af<<" "<<aac<<" "<<atime<<endl; } return 0; }