Happy Programming Contest zoj3703 DP

Source: Internet
Author: User

Description
In Zhejiang University programming Contest, the A team is called "couple team" if it consists the only of the students loving each Other. In the contest, the team would 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 was assigned a value to measure its attracti Veness. Usually, the boy was good at programming while the girl was 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. Him make a plan to solve these problems in strategic order so he can maximize the total attractiveness VA Lue of balloons they get before the contest ends. Under this condition, he wants to solve problems as many as possible. If There is 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's the boy was so clever, he always submit the correct solution.

Input
The first line of input was an integer n (n < indicating) of the number of test cases. For each case, first there are a line containing 2 integers t (t <=) and N (n <=) indicating the contest Leng Th and the number of problems. The next line contains n integers and the i-th an integer ti (ti <=) represents the time needed to solve the ith prob Lem. Finally, there is another line containing n integers and the i-th integer VI (vi <=) 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 pro Blems solved, the total penalty time. The 3 integers should is 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

(1) Test instructions: There is a right to go to the game. Brought a girl. Each question has a value that attracts the girl. Give you the total time, some questions, each question to give you the time spent, the attraction value. And then ask you in the total time, choose a solution, so that the most attractive value, if there are many cases, the choice of more than the number of questions, if there are many cases, choose less penalty. (Don't care if you can win)
(2) Solution: knapsack problem. The main point is the following several comparative relations; If you look at the first condition, the bare 01 backpack, but then the remaining 2 conditions, that is, the state transfer to add some conditions: f[v]=max{f[v],f[v-c[i]]+w[i]}. The condition is satisfied and then the state is transferred. First, to satisfy the attractiveness of the situation, and then consider the number of problems, the final consideration of the penalty. Such a single backpack F[v]=max{f[v],f[v-c[i]]+w[i]} becomes a multivariate condition.

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <iostream>#include <queue>using namespace Std;intMain () {intK scanf"%d", &k); while(k--) {intSumtime,n;int  Time[ -],like[ -];intdp[1005]={0};intzs[1005]={0};intjie[1005]={0};intfuck[1005]={0};intI,j; scanf"%d %d", &sumtime,&n); for(i=0; i<n;i++) scanf ("%d",& Time[i]); for(i=0; i<n;i++) scanf ("%d", &like[i]); for(i=0; i<n;i++)//bubbling. By the time from small to large row. Written to the end. Don't want to be a struct t_t { for(j=i;j<n;j++) {if( Time[j]< Time[i]) {intT t= Time[i]; Time[i]= Time[j]; Time[J]=t;                    T=like[i];                    LIKE[I]=LIKE[J];                like[j]=t; }            }        } for(i=0; i<n;i++) { for(j=sumtime;j>= Time[i];j--) {if(dp[j]<dp[j- Time[I]] +like[i])//first meet like Max {dp[j]=dp[j- Time[I]]                    +like[i]; jie[j]=jie[j- Time[I]] +1; zs[j]=zs[j- Time[I]] + Time[i]; fuck[j]=fuck[j- Time[I]] +zs[j- Time[I]] + Time[i]; }Else  if(dp[j]==dp[j- Time[I]] +like[i]) {if(jie[j]<jie[j- Time[I]] +1)//And then solve the problem up to {dp[j]=dp[j- Time[I]]                        +like[i]; zs[j]=zs[j- Time[I]] + Time[i]; fuck[j]=fuck[j- Time[I]] +zs[j- Time[I]] + Time[i]; jie[j]=jie[j- Time[I]] +1; }Else if(jie[j]==jie[j- Time[I]] +1)                    {if(fuck[j]>fuck[j- Time[I]] +zs[j- Time[I]] + Time[i]) At the end of the penalty to meet the minimum {jie[j]=jie[j- Time[I]] +1; zs[j]=zs[j- Time[I]] + Time[i]; fuck[j]=fuck[j- Time[I]] +zs[j- Time[I]] + Time[i]; dp[j]=dp[j- Time[I]]                        +like[i]; }                    }                }            }        }intOutlove=0, outjie=0, outsumtime=0; for(i=0i<=sumtime;i++)//Find the largest {if(Dp[i]>outlove)               {Outlove=dp[i];               Outjie=jie[i];           Outsumtime=fuck[i]; }Else if(Dp[i]==outlove) {if(Jie[i]>outjie)                    {Outlove=dp[i];                    Outjie=jie[i];               Outsumtime=fuck[i]; }Else if(Jie[i]==outjie) {if(Fuck[i]>outsumtime)                        {Outlove=dp[i];                        Outjie=jie[i];                   Outsumtime=fuck[i]; }               }           }        }printf("%d  %d%d \ n", outlove,outjie,outsumtime); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Happy Programming Contest zoj3703 DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.