ZOJ Course Selection System DP
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的更多相关文章
- Course Selection System ZOJ - 3956 01背包+思维
Course Selection System ZOJ - 3956 这个题目居然是一个01背包,我觉得好难想啊,根本就没有想到. 这个题目把题目给的转化为 ans = a*a-a*b-b*b 这个 ...
- ZOJ-3956 Course Selection System,01背包!
Course Selection System 比赛的时候最后20分钟想到了是01背包,奈何没时间推出怎么背. 题意:n门课程,每门课程都有一个h值和c值,现在给出一个happy的定义,所选的课程的h ...
- ZOJ Problem Set - 3822Domination(DP)
ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...
- ZOJ 3956 Course Selection System 背包DP
ZOJ3956 观察数据范围, c的值非常小 只有100 所以c的和也很有限 只有50000 是否可以从这里下手? 对于某一个c的和 我们一定希望h的和最大 才有可能是最终答案. 于是有了类似背包的d ...
- ZOJ 3956 Course Selection System
题意 有n节课可供选择,每节课都有两个值Hi和Ci,如果学生选择了m节课(x1,x2,....,xm),则它的舒适值被定义为: //这里没有公式((lll¬ω¬)),因为那个图片我保存不下来≧ ﹏ ≦ ...
- ZOJ - 3956 Course Selection System 【01背包变形】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 题意 给出N组Hi Ci 然后 要选出若干个 使得 这个式 ...
- ZOJ 1409 communication system 双变量型的DP
这个题目一开始不知道如何下手,感觉很像背包,里面有两个变量,一个带宽B,一个价格P,有n个设备,每个设备有k个可选的器材(只需选一个),每个器材都有自己的B和P, n个设备选n个器材,最终,FB=所有 ...
- ZOJ 3956 Course Selection System [01背包]
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 题意:就是给你Hi,Ci的值,问怎么取使得下面那个式子的值最大: 理 ...
- Communication System(dp)
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25006 Accepted: 8925 ...
随机推荐
- 有哪些逾渗模型percolation?
自wiki 1.第一个研究的模型是伯努利逾渗, 这个模型中所有键bond独立,物理学家称为键逾渗. 2.随机聚类模型,同Ising模型和Potts模型有很多联系 3.在完备图上的键逾渗,属于在随机图上 ...
- 【C/C++】获取当前系统时间
#include<iostream> #include<Ctime> using namespace std; int main() { time_t t; time(& ...
- 使用diskpart命令修复U盘分区
前段时间在论坛上讨论封装PE到u盘里热闹的,就想自己也封装一个,随便下载了一个WIN7的PE封装后发现还不错,本来就是做测试用的,测试完了就想把u盘在恢复成以前的样子,可是发现恢复并不是这么容易 如下 ...
- 解决js 运算 精度缺失
github地址: https://github.com/MikeMcl/big.js
- Watir 简化日常工作实例
公司的官方主页要优化搜索任务,一共有110个独立页面,开发工程师做了以下工作:1. 为所有这些页面的每个图片添加了alt:2. 为页面上的每个标题添加了面包屑:3. 为网站最重要的标题设置H1标签,而 ...
- JavaScript DOM 编程艺术 ---> JavaScript语法
二. JavaScript语法目录 2.1 语法 javaScript代码要通过HTML/XHTML文档才能执行.可以有两种方式完成这一点,第一种是将JavaScript代码放到文档<head ...
- bzoj 1098 [POI2007]办公楼biu——链表
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1098 求补图的连通块大小.与自己没有边的和自己在一个连通块里. 用链表把所有点串起来.先给自 ...
- Flutter实战视频-移动电商-52.购物车_数据模型建立和Provide修改
52.购物车_数据模型建立和Provide修改 根据json数据生成模型类 {,"price":830.0,"images":"http://imag ...
- C# 生成随机阿拉伯数字,或字符串
//TextBox1.Text = TongYong.SuiJi.SuiJiMingZi(2); public class SuiJi { //TextBox1.Text = TongYong.Sui ...
- eclipse + tomcat 开发环境配置
一. 下载tomcat和Eclipse 下载tomcat 下载地址:http://tomcat.apache.org/download-70.cgi 下载后解压如下图 下载eclipse 下载地址:h ...