http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3703
Happy Programming Contest
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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背包
<span style="font-size:18px;">#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; const int maxn=1010; int value[maxn],a[maxn],fv[maxn]; int vis[55][maxn]; int order[55]; int main() { int N,length,n; cin>>N; while(N--) { int time=0,sumtime=0,numbers=0; memset(vis,0,sizeof(vis)); memset(fv,0,sizeof(fv)); cin>>length>>n; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)cin>>value[i]; for(int i=1;i<=n;i++) { for(int j=length;j>=a[i];j--) { if(fv[j]<fv[j-a[i]]+value[i]) { fv[j]=fv[j-a[i]]+value[i]; vis[i][j]=1; } } } int cnt=0; int i=n; int j=length; for(;i>0;i--) { if(vis[i][j]==1) { order[cnt++]=a[i]; j -= a[i]; } } sort(order,order+cnt); for(int i=0;i<cnt;i++) { time +=order[i]; sumtime +=time; } cout<<fv[length]<< " " << cnt << " " << sumtime << endl; } return 0; }</span>
版权声明:本文为博主原创文章,未经博主允许不得转载。