排序+01揹包


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 aiditi points, where ai indicates the initial points, di indicates the points decreased per minute (count from the beginning of the contest), and tistands 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


題意:在T分鐘內有n道題,對於每道題你能獲得ai-bi*ti(ai指的是開始的時候該道題的分數,bi指的是該題每分鐘減少bi分,ti指的是從開始做這道題到做完多少時間)

問在T時間內最多能夠得到多少分


分析:看到在一個限制條件下,獲得最多多少,就是揹包問題,但是

普通的揹包問題是不用考慮順序問題的,因爲普通的揹包只是有質量和價值,先把哪個放進揹包是一樣的,但是在這裏是不行的,因爲在這裏還有和先後有關係的東西----->>

每分鐘減少的分數和這道題做完需要的時間


所以需要按照 單位時間分數減少最快的 順序排序,即di/ci



#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 6005;

int dp[maxn];
struct node
{
	int a;
	int d;
	int c;
	double ave;
}tmp[maxn];
int cmp(node a,node b)
{
	return a.ave > b.ave;
}
int main()
{
	int n,T;
	cin >> n >> T;
    for(int i = 0;i < n;i++)
    	cin >> tmp[i].a;
    for(int i = 0;i < n;i++)
    	cin >> tmp[i].d;
    for(int i = 0;i < n;i++)
    	cin >> tmp[i].c;
    for(int i = 0;i < n;i++)
    	tmp[i].ave = 1.0*tmp[i].d/tmp[i].c;
    sort(tmp,tmp+n,cmp);
    memset(dp,0,sizeof(dp));
    int mmax = 0;
    for(int i = 0;i < n;i++)
    {
    	for(int j = T;j >= tmp[i].c;j--)
    	{
    		dp[j] = max((dp[j-tmp[i].c]+(tmp[i].a-tmp[i].d*j)),dp[j]);
    		mmax = max(mmax,dp[j]);
    	}
    }
    cout << mmax << endl;
	return 0;
}








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章