acm省赛K题--01背包

Description

LYD loves codeforces since there are many Russian contests. In an contest lasting forT minutes there aren problems, and for the ith problem you can getaiditi points, whereai indicates the initial points,di indicates the points decreased per minute (count from the beginning of the contest), andti stands for the passed minutes when you solved the problem (count from the begining of the contest).
Now you know LYD can solve the ith problem in ci minutes. He can't perform as a multi-core processor, so he can think of only one problem at a moment. Can you help him get as many points as he can?

Input

The first line contains two integers n,T(0≤n≤2000,0≤T≤5000).
The second line contains n integers a1,a2,..,an(0<ai≤6000).
The third line contains n integers d1,d2,..,dn(0<di≤50).
The forth line contains n integers c1,c2,..,cn(0<ci≤400).

Output

Output an integer in a single line, indicating the maximum points LYD can get.

Example Input
3 10
100 200 250
5 6 7
2 4 10
Example Output
254
Hint
Author
“浪潮杯”山东省第八届ACM大学生程序设计竞赛

题意

一个比赛中有n个题,限制时间为T,现在已知你做第i题需要时间为di,得到的分数为ai-di*ti,其中ti为完成这道题的时间

每次只能同时做一道题,求比赛能得到的最大分数。

题解

算是比较简单的01背包吧,反正就是看完题就能有大体思路吧。
不过要注意的是,这个题是跟次序有关的,要对ai,di,ci进行排序。刚开始没注意这点,用三个数组做的,然后wa了好久...
题目抽象:容量T,个数为n,每个占的容量为di,价值跟时间有关,为ai-di*t。
可以放在结构体里,跟J题差不多排序,注意要按照di/ci倒序排序,因为di越大、ci越小,消耗的分数越大,应该尽量往前放

可以称di为增长系数吧,根据背包最优策略求一下就好了


然后还要主要的一个坑,因为ai-di*ci可能是负数,所以最优策略的时候

不能就直接dp[t] = max ( dp[i], dp[i-c[i]] + a[i]-d[i]*t ), 要将dp[i-c[i]]与a[i] - d[i] * t分开算,讨论a[i] - d[i] * t的正负,即max(num[i].a-num[i].d*t,0),再加上dp[i-c[i]],即dp[t-num[i].c]+max(num[i].a-num[i].d*t,0),

所以,总的式子就是:dp[t]=max(dp[t],dp[t-num[i].c]+max(num[i].a-num[i].d*t,0));

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
/*
 * @author Smilewei
 */
struct contest
{
    int a,d,c;
}con[5010];
bool cmp(contest x,contest y)
{
    return (1.0*x.d/x.c)>(1.0*y.d/y.c);
}
int main()
{
    int n,T;
    scanf("%d%d",&n,&T);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&con[i].a);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&con[i].d);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&con[i].c);
    }
    int dp[5010];
    for(int i=0;i<T;i++)
    {
        dp[i]=0;
    }
    sort(con,con+n,cmp);
    int maxn=0;
    for(int i=0;i<n;i++)
    {
        for(int t=T;t>=con[i].c;t--)
        {
            dp[t]=max(dp[t],dp[t-con[i].c]+max(con[i].a-con[i].d*t,0));
            if(maxn<dp[t])maxn=dp[t];
        }
    }
    cout<<maxn<<endl;

    return 0;
}

题目链接:

http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2164/pid/3903

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值