第八届省赛K题(贪心+01背包)

来源:互联网 发布:网络好但是迅雷下载慢 编辑:程序博客网 时间:2024/06/03 18:02

CF

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

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 aiditipoints, 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 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 10100 200 2505 6 72 4 10

Example Output

     254
为了这道题认真的学习了01背包,两题还是有点区别的,该题的价值数随时间的变化而变化
01背包的代码
#include<stdio.h>int main(){int n, v;int i, j;    int val[1002] = { 0 }, w[1002] = { 0 };    int dp[1002]= { 0 };int t,k;while(scanf("%d",&t)!=EOF)    {    while (t--)    {scanf("%d%d", &n, &v);for (i = 1; i <= n; i++)scanf("%d", &val[i]);for (i = 1; i <= n; i++)scanf("%d", &w[i]);for (i = 1; i <= n; i++){for (j = v; j >= w[i]; j--){if (dp[j - w[i]] + val[i] > dp[j]){dp[j] = dp[j - w[i]] + val[i];}}for(k=0;k<=v;++k)                {                    if(k==v) printf("%d\n",dp[k]);                    else printf("%d ",dp[k]);                }}printf("%d\n", dp[v]);    }    }return 0;}
该题的代码
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int max(int a,int b){    return (a>b)?a:b;}typedef struct{   int a,b,c;   float f;}num;bool cmp(num n,num m){    return n.f>m.f;}num node[2005];int main(){    int dp[5005]={0};    int n,t;    cin>>n;cin>>t;    int i,j;    for(i=0;i<n;++i)    {        cin>>node[i].a;    }    for(i=0;i<n;++i)    {        cin>>node[i].b;    }    for(i=0;i<n;++i)    {        cin>>node[i].c;    }    for(i=0;i<n;++i)    {        node[i].f=(float)node[i].b/(float)node[i].c;//求单位时间消耗分数的快慢    }    sort(node,node+n,cmp);//以单位时间内消耗分数最快的从大到小排序    int k=0;    int x=0;    for(i=0;i<n;++i)    {        for(j=t;j>=node[i].c;--j)        {            dp[j]=max(dp[j],dp[j-node[i].c]+node[i].a-j*node[i].b);            k=max(k,dp[j]);//max一定存在能做的题的时间加起来的对应的数组元素上        }    }    cout<<k<<endl;    return 0;}