题目链接

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. LeetCode OJ--Minimum Path Sum **

    https://oj.leetcode.com/problems/minimum-path-sum/ 对一个grid从左上角到右下角的路径,求出路径中和最小的. 受之前思路的影响,就寻思递归,并且记录 ...

  2. 用Linux完成Oracle自动物理备份

    https://blog.csdn.net/24024846/article/details/115292

  3. Toast问题记录:This Toast was not created with Toast.makeText()

    最近使用自己封装的Toast时,遇到一个问题 java.lang.RuntimeException: This Toast was not created with Toast.makeText() ...

  4. 利用github搭建个人网站

    1.注册一个github  https://github.com/ 2.新建一个仓库  仓库名 用 Owner.github.io 的格式,然后点击创建 3.源码上传至github 安装github桌 ...

  5. Java爬虫系列二:使用HttpClient抓取页面HTML

    爬虫要想爬取需要的信息,首先第一步就要抓取到页面html内容,然后对html进行分析,获取想要的内容.上一篇随笔<Java爬虫系列一:写在开始前>中提到了HttpClient可以抓取页面内 ...

  6. win10 nginx + django +flup 配置

    1 安装 Nginx 官网下载,直接点exe启动即可 2 安装django pip install django 注意 新建立项目时 python *****/django-admin.py star ...

  7. 理解Neural Style

    paperA Neural Algorithm of Artistic Style 在艺术领域,尤其是绘画,艺术家们通过创造不同的内容与风格,并相互交融影响来创立独立的视觉体验.如果给定两张图像,现在 ...

  8. PriorityQueue ,ArrayList , 数组排序

    static class E implements Comparable<E>{ int x ; int y ; int state ; int money ; public E(int ...

  9. How to set the initial value of a select element using AngularJS ng-options & track by

    原文: https://www.gurustop.net/blog/2014/01/28/common-problems-and-solutions-when-using-select-element ...

  10. Word Ladder II——找出两词之间最短路径的所有可能

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...