2017第八届浪潮杯山东省赛 K题 codeforces 贪心+背包

来源:互联网 发布:独立思考 知乎 编辑:程序博客网 时间:2024/06/07 00:26

codeforces

发布时间: 2017年5月13日 22:40   最后更新: 2017年5月14日 16:39   时间限制: 1000ms   内存限制: 128M

Clipboard Image.png

LYD loves codeforces since there are many Russian contests. In an contest lasting for T minutes there are n problems, and for the ith problem you can get ai−di∗ti points, where ai indicates the initial points, di indicates the points decreased per minute (count from the beginning of the contest), and ti stands for the passed minutes when you solved the problem (count from the beginning 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 ashe can?

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 an integer in a single line, indicating the maximum points LYD can get.

 复制
3 10100 200 2505 6 72 4 10
254
题意:cf上做题,总共有n个题目,每个题目有对应的a,d,c 其中c是解决这个问题需要的时间,a和d决定解决这个题目能得到的分数,在t时刻解决这个题目可以得到a-t*d的分数,问在T时间内最多能得多少分


第一次接触贪心加背包的题(巨弱。。。。。。一直写裸的01背包,还想不出来错,以为背包都可以跑出来。。。。。。。浪费了很长时间

举一个很简单的例子两个题目  AB,给定时间内都可以解决(T>=c1+c2),先解决A后解决B可以得到的分数是(a1-c1*d1+a2-(c1+c2)*d2),而先解决B后解决A可以得到的分数是(a1-(c1+c2)*d1+a2-c2*d2),两者不同的地方是 -c1*d2   -c2*d1   ,所以应该是d越大的越靠前,c越小的越靠前,这样首先根据d/c 的值排序,再背包

#include<bits/stdc++.h>using namespace std;struct node{    int a,d,c;}num[2005];int dp[5005];int cmp(node u,node v){    return u.c*v.d<v.c*u.d;}int main(){    int T,n,i,j;    cin>>n>>T;    for(i=0;i<n;i++)        scanf("%d",&num[i].a);    for(i=0;i<n;i++)        scanf("%d",&num[i].d);    for(i=0;i<n;i++)        scanf("%d",&num[i].c);    sort(num,num+n,cmp);    int ans=0;    for(i=0;i<n;i++)    {        for(j=T;j>=num[i].c;j--)        {            dp[j]=max(dp[j],dp[j-num[i].c]+max(num[i].a-num[i].d*j,0));            ans=max(ans,dp[j]);        }    }    cout<<ans<<endl;    return 0;}




阅读全文
0 0
原创粉丝点击