题干:

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.

解题报告:

容易观察到对于一个给定的ci,hi肯定是越大越好的,所以我们可以转化成容量是sum(ci),价值分别是hi的01背包问题,这和那道蓝桥有异曲同工之妙。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int dp[MAX];
int h[MAX],c[MAX];
int main()
{int t,n;cin>>t;while(t--) {scanf("%d",&n);int m=0;for(int i = 1; i<=n; i++) {scanf("%d%d",h+i,c+i);m += c[i];}for(int i = 0; i<=m; i++) dp[i] = 0;for(int i = 1; i<=n; i++) {for(int j = m; j>=c[i]; j--) {dp[j] = max(dp[j],dp[j-c[i]]+h[i]);}}ll ans = 0;for(int i = 0; i<=m; i++) {ans = max(ans,1LL*dp[i]*dp[i]-1LL*dp[i]*i-1LL*i*i); }printf("%lld\n",ans);}   return 0 ;
}

【ZOJ - 3956】Course Selection System(01背包)相关推荐

  1. ZOJ 3703 Happy Programming Contest 0-1背包 DP

    ZOJ 3703 Happy Programming Contest 题目描述: 题目链接:ZOJ 3703 Happy Programming Contest 题目大意: 这是一道虐狗的题目.讲的是 ...

  2. 【ZOJ - 3703】Happy Programming Contest(带优先级的01背包,贪心背包)

    题干: In Zhejiang University Programming Contest, a team is called "couple team" if it consi ...

  3. HDU 3339 In Action 最短路+01背包

    题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  4. 【01背包的k值问题 HDU2639 HDU2126】

    HDU2639 有深度吧感觉还 题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值 终结第k大的01背包, 复杂度: O(NMK) 注意选取的标准,选取后,要保证得到的价值是大于未选之前 ...

  5. Java 01背包【动态规划·蓝桥杯练习题】(相信杨超越,相信锦鲤,默默努力,其它的看天意)

    锦鲤镇楼 1.题目描述: 时间限制:1.0s 内存限制:256.0MB 关键字:01背包 动态规划 问题描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高 ...

  6. C语言(CED)01背包——动态规划第二题

    一.问题描述 给定n种物品和一个背包.物品i的质量Wi,其价值Vi,背包的容量为c.问如何选择装入背包中的物品,使得装入背包中的物品总价值最大? 二.解题思想 01背包和最长公共子序列都是动态规划题目 ...

  7. Vijos 1334 ---- NASA的食物计划(01背包强化)

    背景 NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安全技术问题一直大伤脑筋,因此在各方压力下终止了航天飞机的历史,但是此类事情会不会在以后发生,谁也无法保证,在遇到这类航天问题时,解决方法也许 ...

  8. 【恋上数据结构】动态规划(找零钱、最大连续子序列和、最长上升子序列、最长公共子序列、最长公共子串、0-1背包)

    动态规划(Dynamic Programming) 练习1:找零钱 找零钱 - 暴力递归 找零钱 - 记忆化搜索 找零钱 - 递推 思考题:输出找零钱的具体方案(具体是用了哪些面值的硬币) 找零钱 - ...

  9. 0-1 背包实际应用

    0-1 背包实际应用 1.题目要求 From DJ 有许多程序员都热爱玩游戏,而小J自称为游戏王,曾玩过几百种游戏,几乎所有能玩到的游戏大作都玩遍了.随着时间的推移,他发觉已经没有游戏可以让他玩了!于 ...

最新文章

  1. 电路纹身-疼痛的快乐
  2. gson 自定义对象转换格式
  3. 如何向微软 Docs 和本地化社区提交翻译贡献
  4. 作者:姚登举(1980-),男,哈尔滨理工大学副教授。
  5. 数据结构 —— 数据结构相关
  6. python常见排序算法解析
  7. java 代码转换_java字符编码转换过程(转)
  8. iOS底层探索之多线程(一)—进程和线程
  9. 麦库记事如何导出html,麦库记事使用问题解答
  10. 用代码在最短时间做成事
  11. 用聚合数据API快速写出小程序(苏州实时公交)
  12. 99元包月,每周鲜花送上门,看好!
  13. (swing读书笔记)Swing Look And Feel(3)
  14. [李宏毅机器学习笔记]
  15. 跨考计算机应该怎么学,跨考计算机考研经验分享
  16. 超流水线计算机原理,6计算机组成原理第6章流水线原理.ppt
  17. Jenkins Maven打包Jar,部署远程服务器
  18. True FFS内核编程(简单笔记)
  19. FCPX插件直接导入OBJ三维模型FCPX3D MODEL
  20. Solaris 10 Oracle Solaris Studio 12.3 install

热门文章

  1. 【Breadth-first Search 】103. Binary Tree Zigzag Level Order Traversal
  2. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol52]48.TPM的目的和使用方法
  3. outlook2013邮箱找不到服务器,outlook发邮件总提示找不到
  4. 弟子规python编程游戏_《Python游戏趣味编程》 第11章 消灭星星
  5. c语言代码可以python运行吗_c语言如何运行python脚本
  6. delphi 中如果不进行 closehandle 会怎么样_心理学:当你迷茫了,请坚持做三件事,你的未来会越来越好...
  7. idea中没有j2ee_idea神器功能大全
  8. android android 修改 jpg exif 属性,Android开发之使用ExifInterface获取拍照后的图片属性...
  9. 解决asterisk sip呼叫 488 no acceptable here
  10. linux C之access函数