Happy Programming Contest zoj3703 dp

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<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
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<n;i++)  scanf("%d",&time[i]);
        for(i=0;i<n;i++)  scanf("%d",&like[i]);

        for(i=0;i<n;i++)  //冒泡 。按时间从小到大排。写到最后了。不想写成结构体了T_T
        {
            for(j=i;j<n;j++)
            {
                if(time[j]<time[i])
                {
                    int t;
                    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])   //最先满足like最大
                {
                    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)//然后满足解决问题最多
                    {
                        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])//最后满足罚时最少
                        {
                            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\n",outlove,outjie,outsumtime);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 23:21:42

Happy Programming Contest zoj3703 dp的相关文章

ACM International Collegiate Programming Contest, JUST Collegiate Programming Contest (2018)

ACM International Collegiate Programming Contest, JUST Collegiate Programming Contest (2018) B. New Assignment 有n个人(1?≤?n?≤?104),有男有女,每个人都有一个id,现在这n个人分成学习互助小组,有三种组队模式,一个男人一组,一个女人一组,一男一女一组,如果要一男一女一组,那么这两人id的gcd要>1.保证任意三个人的gcd=1.求小组的组数最少是多少? 看起来是一个很裸的二

2018 German Collegiate Programming Contest (GCPC 18)

2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bit

2018 ACM-ICPC, Syrian Collegiate Programming Contest

2018 ACM-ICPC, Syrian Collegiate Programming Contest A Hello SCPC 2018! 水题 B Binary Hamming 水题 C Portals 思路:并查集维护连通性 代码: //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error &quo

(寒假GYM开黑)2018 German Collegiate Programming Contest (GCPC 18)

layout: post title: 2018 German Collegiate Programming Contest (GCPC 18) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 付队博客 C.Coolest Ski Route (记忆化搜索) 题意 给出一个有向图,求出一个权值最长的链, 题解 暴力dfs会超时,所以直接储存每个起点能走到的最远距离 #include<

(寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest

layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 付队! 许老师! Hello SCPC 2018! (签到) #include<bits/stdc++.h> using namespace std; typedef

(寒假开黑gym)2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017)

layout: post title: (寒假开黑gym)2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 付队! 许老师! B.Buildings (polya定理) 题意 B:给你m面墙,每面墙是n*n的格子,你有c种颜色,问你有多少种涂色方案.用po

(寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题)

layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces - DP - 状态压缩 - LCA 传送门 付队! C - Greetings! (状态压缩) 题意 给N种信件,你可以任意选择K种信封装信件,问你最少的浪费是多少 不能大的信件装

【AtCoder】diverta 2019 Programming Contest

diverta 2019 Programming Contest 因为评测机的缘故--它unrated了.. A - Consecutive Integers #include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define

AtCoder hasi&#39;s botsuneta programming contest

目录 A - 1→1 title analysis code B - 01:01 title analysis code C - 1=0.999... title analysis code D - 1+1 title analysis code A - 1→1 title LUOGU AT180 简化题意: \(m\) 表示变化规则的数量,\(n\) 表示要生成 \(1\) 的数量. 对于生成规则 \(a_i,b_i\) 而言,它表示可以将原字符串中的 \(a_i\) 个 \(1\) 变为 \