程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Happy Programming Contest zoj3703 dp

Happy Programming Contest zoj3703 dp

編輯:關於C++

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

(1)題意:有個對要去比賽。帶了個女孩。每個題都有吸引女孩的值。給你總時間,一些題,每個題給你所花時間,吸引力值。然後問你在總時間內,選取一種A題方案,使獲得的吸引力值最大,如果有多種情況,則選擇出題數較多的,如果也有多種情況,選擇罰時較少的。(不在乎是否能贏)
(2)解法:背包問題。本題重點就是後面的幾種比較關系;如果單看第一個條件,就是裸的01背包,但是再處理剩下的2個條件,也就是在狀態轉移中加入一些條件:f[v]=max{f[v],f[v-c[i]]+w[i]}。使得條件滿足的情況下再進行狀態的轉移。先滿足吸引力的情況,再考慮解題數量,最後考慮罰時問題。這樣單一的背包f[v]=max{f[v],f[v-c[i]]+w[i]}變為多元條件。

#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
    int k;
    scanf(%d,&k);
    while(k--)
    {
        int sumtime,n;
        int time[55],like[55];
        int dp[1005]={0};
        int zs[1005]={0};
        int jie[1005]={0};
        int fuck[1005]={0};
        int i,j;
        scanf(%d %d,&sumtime,&n);
        for(i=0;i=time[i];j--)
            {
                if(dp[j]fuck[j-time[i]]+zs[j-time[i]]+time[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];
                            dp[j]=dp[j-time[i]]+like[i];
                        }
                    }
                }
            }
        }
        int outlove=0,outjie=0,outsumtime=0;
        for(i=0;i<=sumtime;i++)  //找出最大的
        {
           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
,outlove,outjie,outsumtime);
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved