K题:CF
题目描述
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?
输入
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 10 100 200 250 5 6 7 2 4 10
样例输出
254
题意 : n 道题目 每道题目都有初始值 ai , 每个单位时间这道题的分数便会减少 di 而我们可以在 ci 时间内做出这道题而得到分数 求在时间 T 内 最多能获得多少 分数
思路:假设每道题做出所需要的时间都为 t ,那么我们一定会选择单位时间内消耗最快的题目开始做,因为这样才能保证总的分数不会减少太快。
假设每道题目不自动消耗分数,那么我们一定会选择题目越快做出越好。
于是,综合以上两种因素得出的公式便是 fi = di/ci ; 根据它排序以后便是裸的01背包啦~
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <cstring> using namespace std ; #define LL long long #define maxn 2500 struct node{ int a ; int d ; int c ; double f ; }; node num[maxn*3] ; bool cmp (node a , node b){ return a.f > b.f ; // 仔细想 为什么这么排序 } int main(){ int n , t ; LL dp[maxn*3] ; while(~scanf("%d%d" , &n , &t)){ memset(dp , 0 , sizeof(dp)) ; for(int i=0 ; i<n ; i++){ scanf("%d" , &num[i].a) ; } for(int i=0 ; i<n ; i++){ scanf("%d" , &num[i].d) ; } for(int i=0 ; i<n ; i++){ scanf("%d" , &num[i].c) ; num[i].f = 1.0*num[i].d/num[i].c ; } sort(num , num+n , cmp) ; for(int i = 0 ; i<n ; i++){ for(int j=t ; j>=num[i].c ; j--){ dp[j] = max(dp[j] , dp[j-num[i].c] + num[i].a - j * num[i].d) ; } } LL result = 0 ; for(int i=0 ; i<=t ; i++){ result = max(result , dp[i]) ; } printf("%d " , result ) ; } return 0 ; }