Hdu 5501 01背包

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5501

The Highest Mark

Problem Description

The SDOI in 2045 is far from what it was been 30 years ago. Each competition has t minutes and n problems.

The ith problem with the original mark of Ai(Ai≤106),and it decreases Bi by each minute. It is guaranteed that it does not go to minus when the competition ends. For example someone solves the ith problem after x minutes of the competition beginning. He/She will get Ai−Bi∗x marks.

If someone solves a problem on x minute. He/She will begin to solve the next problem on x+1 minute.

dxy who attend this competition with excellent strength, can measure the time of solving each problem exactly.He will spend Ci(Ci≤t) minutes to solve the ith problem. It is because he is so godlike that he can solve every problem of this competition. But to the limitation of time, it's probable he cannot solve every problem in this competition. He wanted to arrange the order of solving problems to get the highest mark in this competition.

Input

There is an positive integer T(T≤10) in the first line for the number of testcases.(the number of testcases with n>200 is no more than 5)

For each testcase, there are two integers in the first line n(1≤n≤1000) and t(1≤t≤3000) for the number of problems and the time limitation of this competition.

There are n lines followed and three positive integers each line Ai,Bi,Ci. For the original mark,the mark decreasing per minute and the time dxy of solving this problem will spend.

Hint:
First to solve problem 2 and then solve problem 1 he will get 88 marks. Higher than any other order.

Output

For each testcase output a line for an integer, for the highest mark dxy will get in this competition.

Sample Input

1

4 10

110 5 9

30 2 1

80 4 8

50 3 2

Sample Output

88

题目大意:

每道题目有A,B,C三个值,A表示初始分数,B表示每分钟题的分数会减少B,C表示做这道题需要C分钟,数据保证分数不会变为负数。现在给出比赛时长,问安排做题的顺序,求最大得分。

思路:

很清楚的01背包,但是需要注意的是,完成题目的时间不同会产生不同的的得分

现在有A1,B1,C1和A2,B2,C2这两道题,如果先做T1再做T2的得分是A1-B1*C1+A2-B2*(C1+C2),如果先做T2在做T1的得分是A2-B2*C2+A1-B1*(C1+C2),令先做T1再做T2的得分更高些,那么有A1-B1*C1+A2-B2*(C1+C2) >= A2-B2*C2+A1-B1*(C1+C2),解得B1*C2>=B2*C1所以,只要B1*C2>=B2*C1,那么先做题1再做题2的分数就会更高。
所以,我们需要根据这个来排序,再做01背包

This is the code:

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
struct node
{
    LL a;//得分
    LL b;//
    LL c;//时间
    bool operator <(const node &tem)const
    {
        //还要注意这个地方需要用long long 不然乘法会爆
        return b*tem.c>tem.b*c;//注意这个排序,可以自己输出一边就能明白了
    }
}a[1005];
LL dp[3005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        LL n,T;
        scanf("%lld%lld",&n,&T);
        for(int i=1;i<=n;++i)
            scanf("%lld%lld%lld",&a[i].a,&a[i].b,&a[i].c);
        sort(a+1,a+n+1);//需要排序(贪心)
        //排序保证的是时间优先的同时,得分多
        //for(int i=1;i<=n;++i)
            //printf("%lld  %lld  %lld\n",a[i].a,a[i].b,a[i].c);
        //puts("");
        LL ans=-1;
        for(int i=1;i<=n;++i)
        {
            for(int j=T;j>=a[i].c;--j)
            {
                dp[j]=max(dp[j],dp[j-a[i].c]+a[i].a-j*a[i].b);
                ans=max(dp[j],ans);//dp[T]不一定是得分最高
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值