题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956

题意

给出N组Hi Ci 然后 要选出若干个 使得

这个式子的值最大

然后是可以不选的,这个式子的值就是0

思路

因为 Ci 的范围特别小,我们就可以用Ci 来当做容量 进行01背包

其实在做题的时候有一个问题

就是 ci 并不是连续的

如果 给出一组数据

3

10 1

5 1

2 10

那个ci 的最大就是 12

但是 其实有效值 能够选择的 Ci 其实 就是 1 2 12

那么中间的其他数值会不会导致答案错误

其实是没必要担心的

在中间其他值的过程中 Hi 是上一组的数据 也就是说 在Hi 相同的情况下 这些选不到的Ci的情况下 按那个式子得出的值是更低的,也就是说不会对答案有什么贡献

最后 FOR 一遍 更新答案

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 5e2 + 5;
const int MOD = 1e9 + 7; int h[maxn], c[maxn];
ll dp[maxn * 100]; int main()
{
int t;
cin >> t;
while (t--)
{
int n;
scanf("%d", &n);
int sum = 0;
for (int i = 0; i < n; i++)
{
scanf("%d%d", &h[i], &c[i]);
sum += c[i];
}
CLR(dp);
for (int i = 0; i < n; i++)
{
for (int j = sum; j >= c[i]; j--)
dp[j] = max(dp[j], dp[j - c[i]] + h[i]);
}
ll ans = 0;
for (int i = 1; i <= sum; i++)
ans = max(ans, dp[i] * dp[i] - i * dp[i] - i * i);
printf("%lld\n", ans);
}
}

ZOJ - 3956 Course Selection System 【01背包变形】的更多相关文章

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

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

  2. ZOJ 3956 Course Selection System 背包DP

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

  3. ZOJ 3956 Course Selection System

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

  4. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  5. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  6. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  8. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  9. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

随机推荐

  1. git移除上一次的commit中误添加的文件

    在使用git进行版本管理时,往往会出现一些误操作,比如将一些不加上传的文件放到了暂存区,即上传到了上一次commit中 比如: commit c134ab90ca7c4daf8bfa22e3ad706 ...

  2. numpy常用函数学习

    目录numpy常用函数学习点乘法线型预测线性拟合裁剪.压缩和累乘相关性多项式拟合提取符号数组杂项点乘法该方法为数学方法,但是在numpy使用的时候略坑.numpy的点乘为a.dot(b)或numpy. ...

  3. Windows10下Apache2.4配置Django

    开发环境 Windows 10 x64 Apache 2.4 x64 Python 2.7.11 x64 Django 1.9.6+ 下载和安装mod_wsgi 到 http://download.c ...

  4. C#中通过反射获取类中非公有成员

    public class NGlbGlobeXComm { public static T GetPrivateField<T>(object instance, string field ...

  5. npm升级所有可更新包

    使用npm管理node的包,可以使用npm update <name>对单个包升级,对于npm的版本大于 2.6.1,可以使用命令: npm install -g 升级全局的本地包. 对于 ...

  6. Ubuntu中一次更改用户名带来的连锁反应

    我是一个ubuntu新手,接触ubuntu半年不到,装系统的时候输入了一个用户名,但是最近突然想更名了,这是悲剧的开始! google:ubuntu change username等相关的关键字,最终 ...

  7. lucene 自定义评分 (给lucene自带的评分*我们filed的系数) 如搜索结果时间的加权

    参见孔浩 lucene 22讲 步骤 1.写一个类继承于 CostomScoreQuery -->覆盖getCostomSorceProvider 方法 2.写一个自己的provider(重写c ...

  8. Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  9. [LeetCode][Java] Best Time to Buy and Sell Stock IV

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  10. MySQL的timeout那点事

    http://www.mysqlops.com/2011/11/24/mysql_timeout.html