2017 山东省赛 CF 傻瓜01背包

来源:互联网 发布:美术生 知乎 编辑:程序博客网 时间:2024/05/22 06:54

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 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
The third line contains n integers d1,d2,..,dn.
The forth line contains n integers c1,c2,..,cn.
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

题意:给你每个题的初始 ai 分数 和 每分钟减少 di分数 和做这题需要 ci时间。

傻瓜01背包。。傻瓜背包加贪心。。以前接触过,一时没想起来。

权值是 di/ci 这个越小的越要首先选因为这个是负面的值。

然后就是普通的01背包。

不过需要知道的是 01背包的一个性质,越先选的再排序上要先放在后面。也可以理解为,最后一个一旦确定,整条路线都会已经确定。所以 把权值小的放在后面。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;int n,t;struct node{    int a,d,c;    double f;}s[101010];int cmp(node a,node b){    return a.f>b.f;}int dp[101010];int main(){    while(cin>>n>>t)    {        for(int i=0;i<n;i++)            cin>>s[i].a;        for(int i=0;i<n;i++)            cin>>s[i].d;        for(int i=0;i<n;i++)        {            cin>>s[i].c;            s[i].f=s[i].d*1.0/s[i].c;        }        int maxx=0;        sort(s,s+n,cmp);        memset(dp,0,sizeof(dp));        for(int i=0;i<n;i++)        {            for(int j=t;j>=s[i].c;j--)            {                dp[j]=max(dp[j],dp[j-s[i].c]+s[i].a-s[i].d*j);                maxx=max(maxx,dp[j]);            }        }        printf("%d\n",maxx );    }}
0 0
原创粉丝点击