ZOJ - 3956 Course Selection System 【01背包变形】
题目链接
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背包变形】的更多相关文章
- ZOJ 3956 Course Selection System [01背包]
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 题意:就是给你Hi,Ci的值,问怎么取使得下面那个式子的值最大: 理 ...
- 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¬ω¬)),因为那个图片我保存不下来≧ ﹏ ≦ ...
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- HDU 2639 Bone Collector II(01背包变形【第K大最优解】)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 【01背包变形】Robberies HDU 2955
http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...
- CF#214 C. Dima and Salad 01背包变形
C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...
- 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 ...
随机推荐
- AC日记——Weird Rounding Codeforces 779b
B. Weird Rounding time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- jquery鼠标点击窗口或浮动层以外关闭层【阻止冒泡事件】
$(".up-list a.th1").click(function(){ $(this).next("ul#up-list-ul").show(); }); ...
- 洛谷——P1086 花生采摘
P1086 花生采摘 题目描述 鲁宾逊先生有一只宠物猴,名叫多多.这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!――熊字”. 鲁宾逊先生和多多都 ...
- k8s的使用入门
1.kubectl命令就是apiserver的客户端工具,可以实现对nodes资源的增删改查. # 描述一个节点的信息 kubectl describe node k8s-node1 # 查看k8s集 ...
- Feign详细使用-Spring Cloud学习第四天(非原创)
文章大纲 一.Feign是什么二.Feign的基本实现三.Feign的继承特性四.Feign配置详解五.项目源码与参考资料下载六.参考文章 一.Feign是什么 前面几篇文章我们详细的介绍了Rib ...
- 结构体和类中属性定义需要static地方
private function Readxxx:Integer;static; public class property XXX:Integer read ReadXXx; Txxx =recor ...
- MVC项目引用备注
新建空的WEB应用 MVC Nuget引用:Microsoft.AspNet.MvcMicrosoft.AspNet.Web.Optimization 可选删除 Microsoft.CodeDom.P ...
- 邁向IT專家成功之路的三十則鐵律 鐵律二十:IT人證照之道-收斂
這是一個各行各業都講究專業證照的世代,彷彿證照只要比別人少一些就感覺自己遜掉了.現今IT領域的證照肯定是所有行業中最複雜的,無論是想求職升遷的還是想轉進IT跑道的,許多人由於受到媒體與廣告的影響,都不 ...
- GOF 23种设计模式-单例模式
• 创建型模式: – 单例模式.工厂模式.抽象工厂模式.建造者模式.原型模式. • 结构型模式: – 适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模 式. • 行为型模式: – ...
- win10拷贝文件卡顿的问题-竟然是winrar搞的
win10拷贝文件卡顿的问题-竟然是winrar搞的 学习了: http://www.w10zj.com/Win10xy/Win10xf_3378.html 没想到你竟然是这样的WinRAR 去除了s ...