Course Selection System 【01背包】

31人阅读 评论(0) 收藏 举报
分类:

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
题意很好理解,课程选或者不选,可以向01背包上考虑,但是没有找到固定的东西
这道题目选择C固定,因为C比较小,而且,可以知道当C固定的时候,要尽可能的选择较大的h。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
struct node
{
    long long int c, h;
}t[12311];
long long int Max(long long int a, long long int b)
{
    if(a>b)
        return a;
    return b;
}
long long int dp[560007];//c*n的数量级
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d", &n);
        long long int sum = 0;
        for(int i=0;i<n;i++)
        {
            scanf("%lld %lld", &t[i].h, &t[i].c);
            sum += t[i].c;
        }
        memset(dp, 0, sizeof(dp));
        for(int i=0;i<n;i++)
        {
            for(int j=sum;j>=t[i].c;j--)//c固定,使h尽可能大
            {
                dp[j] = Max(dp[j-t[i].c]+t[i].h, dp[j]);
            }
        }
        long long int maxn = 0;
       for(int i=0;i<=sum;i++)
       {
           maxn = Max(maxn, dp[i]*dp[i]-dp[i]*i-i*i);//题目所给公式
       }
       printf("%lld\n", maxn);
    }
    return 0;
}
查看评论

Linux根文件系统-file system详解

-
  • 1970年01月01日 08:00

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

题目大意:给出一系列H和对应的C,选其中几个: 记:s = 各h的和; t = 各c的和 使得:s * s - s * t - t * t 最大...
  • loy_184548
  • loy_184548
  • 2017-04-13 14:38:10
  • 461

Zoj 3956 Course Selection System (01背包)

Course Selection System Time Limit: 1 Second      Memory Limit: 65536 KB There are n courses...
  • qq_34374664
  • qq_34374664
  • 2017-04-12 12:03:18
  • 414

浙大校赛- Course Selection System

There are n courses in the course selection system of Marjar University. The i-th course is describe...
  • becky_w
  • becky_w
  • 2017-05-10 22:10:01
  • 72

ZOJ 3956 Course Selection System(01背包)

Course Selection System Time Limit: 1 Second      Memory Limit: 65536 KB There are n courses in t...
  • CillyB
  • CillyB
  • 2017-04-10 23:31:36
  • 565

ZOJ17th校赛I题Course Selection System

题目: Course Selection System Time Limit: 1 Second      Memory Limit: 65536 KB There are n c...
  • winnerCLAY
  • winnerCLAY
  • 2017-04-13 18:01:56
  • 393

HZNU2016年校赛 J.Lakes in HZNU[DFS]

(首先是废话....这道题比赛过程中改过很多次,,也可能是我英语太搓,,导致你们没读懂题意.....写了篇题解,,骗访问量.. 题意:给定了一个n*m的矩阵,矩阵上的每个点是由'*'或者'.'组成,问...
  • Chyllo
  • Chyllo
  • 2016-12-04 19:29:39
  • 275

2016 acm香港网络赛 F.Crazy Driver[贪心]

题意:直线上有n个站点,每个站点间有一条路,这样就是n-1条,每条路都有一个过路费v,每个站点都有一个开放时间t,大于等于t才能通过,问在司机不停的情况下最少要花费多少通过所有站点 思路:比赛时一直想...
  • Chyllo
  • Chyllo
  • 2016-09-13 21:19:22
  • 1331

ZOJ3956

Course Selection System题意是给出n对(Hi , Ci) 需要我们选出m对(m是我们自己选择的)使得 最大看到C[i]的数据范围,大概感觉就是需要枚举的。我们观察一下这个式子...
  • WeYoungg
  • WeYoungg
  • 2017-04-18 15:55:45
  • 197

ZOJ 3956 Course Selection System 01背包

Course Selection System Time Limit: 1 Second      Memory Limit: 65536 KB There are n courses in t...
  • jerans
  • jerans
  • 2017-04-12 20:30:22
  • 563
    个人资料
    持之以恒
    等级:
    访问量: 5万+
    积分: 4004
    排名: 9741