zoj3703

来源:互联网 发布:阿里云 ota 安卓 编辑:程序博客网 时间:2024/05/17 21:59

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 integersT (T <= 1000) andn (n <= 50) indicating the contest length and the number of problems. The next line containsn integers and thei-th integer ti (ti <= 1000) represents the time needed to solve the ith problem. Finally, there is another line containingn integers and thei-th integer vi (vi <= 1000) represents the attractiveness value of thei-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

2300 1010 10 10 10 10 10 10 10 10 101 2 3 4 5 6 7 8 9 10300 10301 301 301 301 301 301 301 301 301 3011000 1000 1000 1000 1000 1000 1000 1000 1000 1000

Sample Output

55 10 5500 0 0


题意:给出比赛时间和题目数,并给出每道题目的耗时以及获得的价值,找出所能获得的最大价值,获得这个最大价值下的最多题目数以及最少罚时,这里的罚时理解的半天,举第一个例子:A掉第一道题需要用时10分钟,然后第二道题也需要10分钟,即第二道题得在20分钟的时候A掉,所以两道题的罚时是10+20

思路:这是一个背包问题,首先得先对每道题根据时间进行从小到大排序,使得最后得到的时候在价值和题目数相同的时候保证时间最短


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct bag{int val,cnt,time;}dp[55][1005];struct sw{int needtime,val;}node[55];int cmp(const void *a,const void *b){return ((sw *)a)->needtime-((sw *)b)->needtime;}int main(void){int T,n,w;scanf("%d",&T);while(T--){scanf("%d%d",&w,&n);for(int i=1;i<=n;i++)scanf("%d",&node[i].needtime);for(int i=1;i<=n;i++)scanf("%d",&node[i].val);memset(dp,0,sizeof(dp));qsort(&node[1],n,sizeof(node[0]),cmp); //贪心,按时间从小到大排序 for(int i=1;i<=n;i++)//从第一道题开始往后推 for(int j=0;j<=w;j++){//从0开始保证所有状态能从上一道题转移下来 dp[i][j]=dp[i-1][j];//还没做出当前i题时的状态 if(j<node[i].needtime) continue;int tval=dp[i-1][j-node[i].needtime].val+node[i].val; //在j时间下,上一道题空出当前i题的时间所获得的最大价值+上当前这道题的价值 int tcnt=dp[i-1][j-node[i].needtime].cnt+1;// 在j时间下,上一道题空出当前i题的时间所获得的最大题数+上当前这道题 if(tval<dp[i][j].val)continue;else if(tval>dp[i][j].val){ //当价值高于没做j时间下没做这道题的价值时,更新做这道题为j状态下最优 dp[i][j].val=tval;dp[i][j].cnt=tcnt;dp[i][j].time=dp[i-1][j-node[i].needtime].time+j; //表示之前的罚时加上现在完成这个的罚时continue; }else {//价值相同下 if(tcnt<dp[i][j].cnt)continue;else if(tcnt>dp[i][j].cnt){ //价值相同下,若题数多,则更新 dp[i][j].cnt=tcnt;dp[i][j].time=dp[i-1][j-node[i].needtime].time+j; //表示之前的罚时加上现在完成这个的罚时continue; }}}int maxval=0,maxcnt=0,mint=0;for(int i=0;i<=w;i++){if(dp[n][i].val>maxval){maxval=dp[n][i].val;maxcnt=dp[n][i].cnt;mint=dp[n][i].time;}else if(dp[n][i].val==maxval&&dp[n][i].cnt>maxcnt){maxcnt=dp[n][i].cnt;mint=dp[n][i].time;}}printf("%d %d %d\n",maxval,maxcnt,mint);}return 0;}


0 0