浙大校赛- Course Selection System

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

这道题题目大意很简单,这道题就是给了一个公式,求最大值:

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;
struct
{
    int h;
    int c;
} cur[505];
ll hmin[50005];
ll hmax[50005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(hmax,0,sizeof(hmax));
        int n,sum;
        sum=0;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&cur[i].h,&cur[i].c);
            sum+=cur[i].c;
        }
      // cout<<sum<<endl;
        hmin[sum]=cur[n].h;
        hmax[sum]=cur[n].h;
        hmax[0]=hmin[0]=0;
       /* for(int i=1; i<=n; i++) { for(int j=sum; j>=1; j--) { hmin[j]=min(hmin[j],hmin[j-cur[i].c]+cur[i].h); } }*/
        for(int i=1; i<=n; i++)
        {
            for(int j=sum; j>=1; j--)
            {
                if(j-cur[i].c>=0)
                hmax[j]=max(hmax[j],hmax[j-cur[i].c]+cur[i].h);
            }
        }
      /* for(int i=1;i<=sum;i++){ cout<<hmax[i]<<" "; }*/
        ll res=0;
        for(int i=1;i<=sum;i++){
         ll ans=hmax[i]*hmax[i]-hmax[i]*i-i*i;
         res=max(res,ans);
        }
        printf("%lld\n",res);
    }
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务