ZOJ3703-Happy Programming Contest

29 篇文章 0 订阅

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

Author:  HUANG, Qiao
Contest:  The 13th Zhejiang University Programming Contest


题意:有个对要去比赛。带了个女孩。每个题都有吸引女孩的值。给你总时间,一些题,每个题给你所花时间,吸引力值。然后问你在总时间内,选取一种A题方案,使获得的吸引力值最大,如果有多种情况,则选择出题数较多的,如果也有多种情况,选择罚时较少的

解题思路:01背包


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <functional>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

struct bag
{
    int val,cnt,time;
}dp[55][1005];

struct node
{
    int needtime,val;
}a[55];

int cmp(node a,node b)
{
    return a.needtime<b.needtime;
}

int main()
{
    int t,n,w;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&w,&n);
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i].needtime);
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i].val);
        memset(dp,0,sizeof dp);
        sort(a+1,a+1+n,cmp);
        for(int i=1; i<=n; i++)
        {
            for(int j=0; j<=w; j++)
            {
                dp[i][j]=dp[i-1][j];
                if(j<a[i].needtime) continue;
                int tval=dp[i-1][j-a[i].needtime].val+a[i].val;
                int tcnt=dp[i-1][j-a[i].needtime].cnt+1;
                if(tval<dp[i][j].val) continue;
                else if(tval>dp[i][j].val)
                {
                    dp[i][j].val=tval;
                    dp[i][j].cnt=tcnt;
                    dp[i][j].time=dp[i-1][j-a[i].needtime].time+j;
                }
                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-a[i].needtime].time+j;
                    }
                }
            }
        }
        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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值