ZOJ-3956-Course Selection System【01背包】【17th浙大校赛】

来源:互联网 发布:黑莓q10软件 编辑:程序博客网 时间:2024/06/06 10:59

There are n courses in the course selection system of Marjar University. The i-th course is described by two values: happiness Hi and credit Ci. If a student selects m courses x1, x2, …, xm, then his comfort level of the semester can be defined as follows:

(i=1mHxi)2(i=1mHxi)×(i=1mCxi)(i=1mCxi)2

Edward, a student in Marjar University, wants to select some courses (also he can select no courses, then his comfort level is 0) to maximize his comfort level. Can you help him?

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains a integer n (1 ≤ n ≤ 500) – the number of cources.

Each of the next n lines contains two integers Hi and Ci (1 ≤ Hi ≤ 10000, 1 ≤ Ci ≤ 100).

It is guaranteed that the sum of all n does not exceed 5000.

We kindly remind you that this problem contains large I/O file, so it’s recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.

Output

For each case, you should output one integer denoting the maximum comfort.

Sample Input
2
3
10 1
5 1
2 10
2
1 10
2 10

Sample Output
191
0

Hint
For the first case, Edward should select the first and second courses.

For the second case, Edward should select no courses.

好灵活的01 背包的题啊,根据C一定,求H的最大值,然后就能求得总的最大值了

#include<cstdio>#include<cstring>#include<stdlib.h>#include<fstream>#include<ctype.h>#include<math.h>#include<stack>#include<queue>#include<map>#include<set>#include<vector>#include<string>#include<iostream>#include<algorithm>#include<utility>#include<iomanip>#include<time.h>#include<iostream>#define lowbit(x) (x&-x)#define abs(x) ((x)>0?(x):-(x))using namespace std;typedef long long ll;const double Pi = acos(-1.0);const int N = 1e6+10, M = 1e3+20, mod = 1e9+7, inf = 2e9+10;const double e=2.718281828459 ;const double esp=1e-9;ll t,n,h[505],c[505];ll dp[N];int main(){    std::ios::sync_with_stdio(false);    std::cin.tie(0);    cin>>t;    while(t--)    {        ll sum1=0;        memset(dp,0,sizeof(dp)); cin>>n;        for(int i=0; i<n;i++)        {            cin>>h[i]>>c[i];              sum1+=c[i];        }        ll sum=0;        for(int i=0;i<n;i++)        {            for(int j=sum1;j>=c[i];j--)            {                dp[j]=max(dp[j],dp[j-c[i]]+h[i]);            }        }        for(int i=0;i<=sum1;i++)        {            sum=max(sum,(ll)dp[i]*dp[i]-(ll)i*i-(ll)i*dp[i]);        }        cout<<sum<<endl;    }    return 0;}
0 0
原创粉丝点击