Cow Exhibition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11092   Accepted: 4404

Description

"Fat and docile, big and dumb, they look so stupid, they aren't much 
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

* Line 1: A single integer N, the number of cows

* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.

Output

* Line 1: One integer: the optimal sum of TS and TF such that both TS and TF are non-negative. If no subset of the cows has non-negative TS and non- negative TF, print 0.

Sample Input

5
-5 7
8 -6
6 -3
2 1
-8 -5

Sample Output

8
题意:给出n个奶牛,每个奶牛有ts之和tf值,从中选出一些奶牛使ts+tf之和最大且ts之和与tf之和均非负.
思路:选与不选的问题,转化为01背包。将ts作为体积,tf作为价值。
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
int dp[MAXN];
int n;
int ts[MAXN],tf[MAXN];
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<MAXN;i++) dp[i]=-INF;
for(int i=;i<n;i++)
{
scanf("%d%d",&ts[i],&tf[i]);
}
dp[]=;
for(int i=;i<n;i++)
{
if(ts[i]<&&tf[i]<) continue;
if(ts[i]>)
{
for(int j=;j>=ts[i];j--)//体积大于0时倒序
dp[j]=max(dp[j],dp[j-ts[i]]+tf[i]);
}
else
{
for(int j=;j<=+ts[i];j++)//体积小于0时正序
dp[j]=max(dp[j],dp[j-ts[i]]+tf[i]);
}
}
int maxn=-INF;
for(int i=;i<=;i++)
{
if(dp[i]>=)
maxn=max(maxn,dp[i]+i-);
} if(maxn>) printf("%d\n",maxn);
else printf("0\n"); } return ;
}

POJ2184(01背包变形)的更多相关文章

  1. poj2184 01背包变形,价值为可为负数

    题目链接:http://poj.org/problem?id=2184 题意:每行给出si和fi,代表牛的两个属性,然后要求选出几头牛,满足S与F都不能为负数的条件下,使S与F的和最大. tips:动 ...

  2. FZU 2214 Knapsack problem 01背包变形

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

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

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

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

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

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

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

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

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

  7. POJ-2184 Cow Exhibition(01背包变形)

    Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...

  8. 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 ...

  9. POJ 3211 Washing Cloths(01背包变形)

    Q: 01背包最后返回什么 dp[v], v 是多少? A: 普通01背包需要遍历, 从大到小. 但此题因为物品的总重量必定大于背包容量, 所以直接返回 dp[V] 即可 update 2014年3月 ...

随机推荐

  1. node开发自动刷新网页中的css和javascript

    在已有node的环境下,安装browser-sync: npm install -g browser-sync 然后运行,默认本目录下(最后填写要监听的文件--本实例监听了css文件夹下面的所有css ...

  2. PHP如何学习?

    PHP 的学习,可以归纳为三个类型:      语言的基础语法学习,这些是 ifelse, while, switch, class, function, trait 等:  内置函数/类学习,这 ...

  3. 常见Struts、Hibernate、Spring、J2EE、ibatis、Oracle等开发框架架构图及其简介

    各种系统架构图及其简介 转载请保留出处,不胜人生一场醉汇总. 以下文字和架构图均在本人相关系统设计和架构方案中有所应用. 原文出处:http://space.itpub.net/6517/viewsp ...

  4. Windows下利用CMake和VS2013编译OpenCV

    转载自:http://www.chengxulvtu.com/2014/03/19/windows_build-opencv-with-cmake-and-vs2013.html   获取OpenCV ...

  5. 加载jsp页面报#{} is not allowed in template text

    问题是在引进jQueryUI时遇到 解决方法: 在page指令添加:             deferredSyntaxAllowedAsLiteral="true" 例如:&l ...

  6. 2016年最值得新手程序猿阅读的书:《增长project师指南》

    这本书的来源于根据我在<Repractise简单介绍篇:Web开发的七天里>中所说的 Web 开发的七个步骤而展开的电子书.当然它也是一个 APP.它一本关于怎样成为增长project师的 ...

  7. 侧边打赏-html

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  8. 08 comet反向ajax

    一:HTTP协议与技久链接+分块传输---->反向ajax 反向ajax又叫comet, server push,服务器推技术. 应用范围: 网页聊天服务器,, 新浪微博在线聊天,google ...

  9. 多媒体开发之编码gop---什么是GOP

    所谓GOP,意思是画面组,MPEG格中的帧序列,分为I.P.B三种,如排成IBBPBBPBBPBBPBBP...样式,这种连续的帧图片组合即为GOP(画面群,GROUP OF PICTURE),是MP ...

  10. 【原创】Sublime Text 3快捷配置c++的编译,运行,gdb环境

    打开Tools ->Build System -> New Build System 弹出一个文件,将原有的东西删掉,输入: { "encoding": "u ...