http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5565

Course Selection System


Time Limit: 1 Second      Memory Limit: 65536 KB

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:

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.


Submit    Status

设sigma(H[i]) = a,sigma(c[i]) = b

那么原式 = a * (a - b) - b * b

对于固定b值,我们希望a值越大越好。

一开始的时候设dp[i][j][b]表示前i们科目,选了j们,产生的b值是b的时候,的最大a值。

然后发现这样会超时。观察到选了多少门我们是并不关心的。这样可以省掉500

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = * + ;
int dp[maxn];
int h[maxn], c[maxn];
void work() {
memset(dp, , sizeof dp);
int n;
scanf("%d", &n);
int tot = ;
for (int i = ; i <= n; ++i) {
scanf("%d%d", &h[i], &c[i]);
tot += c[i];
}
for (int i = ; i <= n; ++i) {
for (int j = tot; j >= c[i]; --j) {
dp[j] = max(dp[j], dp[j - c[i]] + h[i]);
}
}
LL ans = ;
for (int i = ; i <= tot; ++i) {
LL t = 1LL * dp[i] * dp[i] - 1LL * dp[i] * i - 1LL * i * i;
ans = max(ans, t);
}
printf("%lld\n", ans);
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

ZOJ Course Selection System DP的更多相关文章

  1. Course Selection System ZOJ - 3956 01背包+思维

    Course Selection System ZOJ - 3956 这个题目居然是一个01背包,我觉得好难想啊,根本就没有想到. 这个题目把题目给的转化为  ans = a*a-a*b-b*b 这个 ...

  2. ZOJ-3956 Course Selection System,01背包!

    Course Selection System 比赛的时候最后20分钟想到了是01背包,奈何没时间推出怎么背. 题意:n门课程,每门课程都有一个h值和c值,现在给出一个happy的定义,所选的课程的h ...

  3. ZOJ Problem Set - 3822Domination(DP)

    ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...

  4. ZOJ 3956 Course Selection System 背包DP

    ZOJ3956 观察数据范围, c的值非常小 只有100 所以c的和也很有限 只有50000 是否可以从这里下手? 对于某一个c的和 我们一定希望h的和最大 才有可能是最终答案. 于是有了类似背包的d ...

  5. ZOJ 3956 Course Selection System

    题意 有n节课可供选择,每节课都有两个值Hi和Ci,如果学生选择了m节课(x1,x2,....,xm),则它的舒适值被定义为: //这里没有公式((lll¬ω¬)),因为那个图片我保存不下来≧ ﹏ ≦ ...

  6. ZOJ - 3956 Course Selection System 【01背包变形】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 题意 给出N组Hi Ci 然后 要选出若干个 使得 这个式 ...

  7. ZOJ 1409 communication system 双变量型的DP

    这个题目一开始不知道如何下手,感觉很像背包,里面有两个变量,一个带宽B,一个价格P,有n个设备,每个设备有k个可选的器材(只需选一个),每个器材都有自己的B和P, n个设备选n个器材,最终,FB=所有 ...

  8. ZOJ 3956 Course Selection System [01背包]

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 题意:就是给你Hi,Ci的值,问怎么取使得下面那个式子的值最大: 理 ...

  9. Communication System(dp)

    Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25006 Accepted: 8925 ...

随机推荐

  1. hdu 1029 Ignatius and the Princess IV(排序)

    题意:求出现次数>=(N+1)/2的数 思路:排序后,输出第(N+1)/2个数 #include<iostream> #include<stdio.h> #include ...

  2. spring mvc提交日期类型参数

    如题,spring mvc直接提交Date类型参数会报错,400 bad request的错误.在controller里加上 @InitBinder protected void initBinder ...

  3. codeforces 665B B. Shopping(水题)

    题目链接: B. Shopping time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. Quicklz压缩算法

    以前对压缩算法一无所知,只是知道哈弗曼编码能做这种事情,但是感觉这样的方法奇慢无比.昨天下午看了下号称世界上最快的压缩算法Quicklz,对压缩的基本思路有了一定的了解.一般的压缩程序的要求读入文件之 ...

  5. bzoj2330糖果——差分约束

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束,再建立一个源点0,向所有点连边权为1的边,表示每个人都会分到糖果: 答案较大 ...

  6. HDU3466(01背包变种)

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  7. 管理SSIS 日志

    转自:http://www.cnblogs.com/biwork/p/biworklog.html 一直准备写这么一篇有关 SSIS 日志系统的文章,但是发现很难一次写的很完整.因为这篇文章的内容可扩 ...

  8. java枚举类Enum方法简介(valueof,value,ordinal)

    Enum作为Sun全新引进的一个关键字,看起来很象是特殊的class,   它也可以有自己的变量,可以定义自己的方法,可以实现一个或者多个接口.   当我们在声明一个enum类型时,我们应该注意到en ...

  9. JS正则表达式(一)

    正则表达常用符号 /..../  开始结束 ^ 开始 $ 结束 /s 任何非空字符  /S 非空 /d  匹配一个数字=[0-9] /D  匹配一个非数字=[^0-9] /w   匹配一个数字,下划线 ...

  10. app UI测试之UIAutomator

    执行UIAutomator测试步骤 1.新建Java项目,导入android.jar和uiautomator.jar包,继承UiAutomatorTestCase 2.生成编译文件 android c ...