POJ 2184 01背包+负数处理
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10200 | Accepted: 3977 |
Description
fun..."
- Cows with Guns by Dana Lyons
The cows want to prove to the public that they are both smart and fun. In order to do this, Bessie has organized an exhibition that will be put on by the cows. She has given each of the N (1 <= N <= 100) cows a thorough interview and determined two values for each cow: the smartness Si (-1000 <= Si <= 1000) of the cow and the funness Fi (-1000 <= Fi <= 1000) of the cow.
Bessie must choose which cows she wants to bring to her exhibition. She believes that the total smartness TS of the group is the sum of the Si's and, likewise, the total funness TF of the group is the sum of the Fi's. Bessie wants to maximize the sum of TS and TF, but she also wants both of these values to be non-negative (since she must also show that the cows are well-rounded; a negative TS or TF would ruin this). Help Bessie maximize the sum of TS and TF without letting either of these values become negative.
Input
* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.
Output
Sample Input
- 5
- -5 7
- 8 -6
- 6 -3
- 2 1
- -8 -5
Sample Output
- 8
Hint
Bessie chooses cows 1, 3, and 4, giving values of TS = -5+6+2 = 3 and TF
= 7-3+1 = 5, so 3+5 = 8. Note that adding cow 2 would improve the value
of TS+TF to 10, but the new value of TF would be negative, so it is not
allowed.
Source
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <cmath>
- #include <set>
- using namespace std;
- #define N 200005
- #define sh 100000
- #define inf 999999999
- int max(int x,int y){return x>y?x:y;}
- int min(int x,int y){return x<y?x:y;}
- int abs(int x,int y){return x<?-x:x;}
- int dp[N];
- struct node{
- int v, w;
- }a[];
- int n;
- main()
- {
- int i, j, k;
- while(scanf("%d",&n)==){
- for(i=;i<n;i++) scanf("%d %d",&a[i].v,&a[i].w);
- for(i=;i<N;i++) dp[i]=-inf;
- dp[sh]=;
- for(i=;i<n;i++){
- if(a[i].v>){
- for(k=N-;k>=a[i].v;k--) dp[k]=max(dp[k-a[i].v]+a[i].w,dp[k]);
- }
- else{
- for(k=;k<N+a[i].v;k++) dp[k]=max(dp[k-a[i].v]+a[i].w,dp[k]);
- }
- }
- int ans=;
- for(i=sh;i<N;i++){
- if(dp[i]>) {
- ans=max(ans,i-sh+dp[i]);
- }
- }
- printf("%d\n",ans);
- }
- }
POJ 2184 01背包+负数处理的更多相关文章
- poj 2184 01背包变形【背包dp】
POJ 2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14657 Accepte ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- poj 1837 01背包
Balance Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u Java clas ...
- hdu 2184 01背包变形
转自:http://blog.csdn.net/liuqiyao_01/article/details/8753686 题意:这是又是一道01背包的变体,题目要求选出一些牛,使smartness和fu ...
- POJ 3628 01背包 OR 状压
思路: 1.01背包 先找到所有奶牛身高和与B的差. 然后做一次01背包即可 01背包的容积和价格就是奶牛们身高. 最后差值一减输出结果就大功告成啦! 2. 搜索 这思路很明了吧... 搜索的确可以过 ...
- Proud Merchants(POJ 3466 01背包+排序)
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- POJ 3624 01背包
初学DP,用贪心的思想想解题,可是想了一个多小时还是想不出. //在max中的两个参数f[k], 和f[k-weight[i]]+value[i]都是表示在背包容量为k时的最大价值 //f[k]是这个 ...
- POJ之01背包系列
poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using na ...
- (01背包变形) Cow Exhibition (poj 2184)
http://poj.org/problem?id=2184 Description "Fat and docile, big and dumb, they look so stupid ...
随机推荐
- Java多线程编程——进阶篇二
一.线程的交互 a.线程交互的基础知识 线程交互知识点需要从java.lang.Object的类的三个方法来学习: void notify() 唤醒在此对象监视器上等待的单个 ...
- js asp.net获取时间戳
方法1: var timestamp = (new Date()).valueOf(); 结果:1280977330748 方法2: var timestamp=new Date().getTime( ...
- VS 编辑并继续(转载)
转]Microsoft Visual Studio vs2008 vs2010 调试 编辑 修改 代码 在vs2008的文件菜单下,前两个菜单项分别是新建项目 和 新建网站. 这两项里,都可以建web ...
- Unity随机随学
1.什么是渲染管道? 是指在显示器上为了显示出图像而经过的一系列必要操作.渲染管道中的步骤很多,都要将几何物体从一个坐标系中变换到另一个坐标系中去. 主要步骤有: 本地坐标->视图坐标-> ...
- 【转】Struts1.x系列教程(5):HTML标签库
转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/0 ...
- Java菜鸟学习 Script 脚本语言(简介)
script 可以写在head里 也可以写在body里 还可以写在 /html后面 script 也是成对出现的 <script></script> 他有三种常见的对话框 1 ...
- poj 1091 跳蚤
跳蚤 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8482 Accepted: 2514 Description Z城 ...
- GZAPI框架初识
新建一个MVC项目(GZAPIFramework.Demo): mvc:用于API接口文档查看,Log日志查看 webapi:api调用 新建一个Biz类库并添加nuget引用: 搜索GZAPI.Co ...
- jQuery EasyUI教程之datagrid应用(一)
最近一段时间都在做人事系统的项目,主要用到了EasyUI,数据库操作,然后抽点时间整理一下EasyUI的内容. 这里我们就以一个简洁的电话簿软件为基础,具体地说一下datagrid应用吧 datagr ...
- 凯撒密码加密C语言简单实现
凯撒加密(Julius Caesar)该方法把一条消息中的每个字母用字母表中固定距离之后的那个字母代替.(如果超越了字母Z,会绕道字母表的起始位置.例如,如果每个字母都用字母表中两个位置之后的字母代替 ...