Cow Exhibition_背包(负数情况)
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.
【题意】给出n头牛,分别给出它们的si,fi;取出几头使得si和fi的和最大,并且si之和与fi之和不能为负数;
【思路】背包题,就是存在负数的情况,可以把负数存入下标,数组开大点。用dp[i]存放每个s[i]能得到的最大的f,根据dp的有无,选出最大的dp
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
const int inf=<<;
int dp[N];
int s[],f[];
void init()
{
for(int i=;i<=;i++)
dp[i]=-inf;
dp[]=;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init();
for(int i=;i<=n;i++)
{
scanf("%d%d",&s[i],&f[i]);
}
for(int i=;i<=n;i++)
{
if(s[i]<&&f[i]<) continue;//两个都为负,没有必要进行下去
if(s[i]>)//si为正,进行从大到小的背包
{
for(int j=;j>=s[i];j--)
{
if(dp[j-s[i]]>-inf)
dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
}
}
else//为负数则从小到大背包
{
for(int j=s[i];j<=+s[i];j++)
{
if(dp[j-s[i]]>-inf)
dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
}
} }
int ans=-inf;
for(int i=;i<=;i++)
//dp[]的范围是100000~200000;i就是s[i],如果此时dp[i]也就是f[i]大于等于0的话,
//再加上s[i]-100000(界限)就是答案
{
if(dp[i]>=) ans=max(ans,dp[i]+i-); }
printf("%d\n",ans);
} return ;
}
Cow Exhibition_背包(负数情况)的更多相关文章
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- POJ-2184 Cow Exhibition---01背包变形(负数偏移)
题目链接: https://vjudge.net/problem/POJ-2184 题目大意: 给出num(num<=100)头奶牛的S和F值(-1000<=S,F<=1000),要 ...
- POJ 2184 01背包+负数处理
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10200 Accepted: 3977 D ...
- POJ2184 Cow Exhibition 背包
题目大意:已知c[i]...c[n]及f[i]...f[n],现要选出一些i,使得当sum{c[i]}和sum{f[i]}均非负时,sum(c[i]+f[i])的最大值. 以sum(c[i])(c[i ...
- LG5196 「USACO2019JAN」Cow Poetry 背包+乘法原理
\(\mathrm{Cow Poetry}\) 问题描述 LG5196 题解 因为每句诗的长度一定是\(k\),所以自然而然想到背包. 设\(opt[i][j]\)代表到第\(i\)位时,结尾为\(j ...
- poj 1837 01背包
Balance Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u Java clas ...
- 背包dp相关
0/1背包 给出n个物品,每个物品有Vi的价值和Wi的费用,我们总共有m块钱,求最多能得到多少价值的物品. N<=10^3,m<=10^3 记录方案数?记录输出方案? 输出方案: 对每个d ...
- Margaritas on the River Walk_背包
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...
- java判断字符串是否为数字,包括负数
/** * 判断是否为数字,包含负数情况 * @param str * @return */ private boolean isNumeric(String str){ Boolean flag = ...
随机推荐
- 如何查看,关闭和开启selinux
以下介绍一下SELinux相关的工具/usr/bin/setenforce 修改SELinux的实时运行模式setenforce 1 设置SELinux 成为enforcing模式setenforce ...
- input file类型,文件类型的限制
直接限制input type='file'的文件类型限制,通过accept属性进行设定,多个类型用逗号分隔开,因为accept是html5的新特性,所以火狐和IE的支持就显得单薄了, 如:
- Cookie实例,理解cookie
一.一句话了解cookie是什么 cookie是服务端发送给客户端的.用来记录一些信息(如用户名),定制主页,聚焦广告的.最终以文件形式存在于客户端电脑磁盘下的小型文档. 二.用实例来认清cookie ...
- NetworkComms框架介绍 序列化并发送对象
NetworkComms网络通信框架序言 英文原文:http://www.networkcomms.net/custom-objects/ NetworkComms.Net网络库,支持发送自定义类,并 ...
- 关于python中的编码:unicode, utf-8, gb2312
计算机早期是只支持ASCII码的,经过long long的发展,出现了这些支持世界上各种语言字符的编码:unicode, utf-8, gb2312. 对于unicode, utf-8, gb2312 ...
- JS正则表达式基础
正则表达式的作用: 测试字符串的某个模式 替换文本 根据模式匹配从字符串中提取一个子字符串.可以用来在文本或输入字段中查找特定文字 [^\d]/g这是一个正则表达式,在JS中 ...
- 时钟 IoTimer
/* 例程是在运行在DISPATCH_LEVEL的IRQL级别 例程中不能使用分页内存 另外在函数首部使用 #pragma LOCKEDCODE */ #include "Driver.h& ...
- 铁人系列 (1) uva 10385
uva 10385 列出n-1个一元方程,对应成单峰函数,所以用三分求解即可. #include <cstdio> #include <cstring> #include & ...
- win7下python安装pyquery
安装pyquery之前首先要明确一点,easyinstall 是一款python包管理器,类似于node的npm,用于安装python的扩展包,它安装的包是以*.egg的方式. 要安装pq需要经历以下 ...
- Oracle ODP.NET 篇
1.C# 使用 System.Data.OracleClient 连接 Oracle 需要安装 instantclient , 并配置相应环境变量.重启,方可使用. 2. 使用 System.Data ...