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. html5--4-3 source元素-解决浏览器的兼容

    html5--4-3 source元素-解决浏览器的兼容 学习要点 掌握source元素的用法 source元素-解决浏览器额兼容 HTML5 中新增的元素 video和audio元素的子元素,可指定 ...

  2. 未知USB设备 端口重置失败

    1.开启手机中USB调试 进入“设置”->“应用程序”->“开发”勾选“USB调试程序”.这样设备才可以通过USB连线时被PC识别到. 2.安装驱动 要将Android手机连接到PC需要安 ...

  3. deepin软件中文乱码

    如图所示,deepin软件,会有这种情况,中文全是乱码,口口口.这表示很讨厌,学长给出的解决办法,将系统换成英文语言,这样确实解决了乱码,但是还是有问题,比如在写中文注释,又变成这样了. 去deepi ...

  4. hdu-5665 Lucky(水题)

    题目链接: Lucky Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Pro ...

  5. Linux GCC常用命令学习

    1简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objective ...

  6. java中的异常The given object has a null identifier

    修改页面点击提交时报如下异常: org.hibernate.TransientObjectException: The given object has a null identifier: com. ...

  7. Makefile的常用技术总结

    一.MAKE中的自动变量:    $@: 表示target的名字    $%: 仅当目标是函数库文件中,表示规则中的目标成员名.例如,如果一个目标是"foo.a(bar.o)",那 ...

  8. centos7 安装 python3.5

    centos7 安装 python3.5 一. python虚拟环境virtualenv VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前 ...

  9. Ubuntu12.04下安装、使用、卸载MySQL

    转自:http://blog.csdn.net/yimi0903/article/details/11800713 一.安装 Step1:安装MySQL-server,mysql-client 执行以 ...

  10. 解决At least one JAR was scanned for TLDs yet contained no TLDs. 问题

    启动tomcat运行项目时,总是提示: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug loggin ...