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 = ...
随机推荐
- Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- 20145236 《Java程序设计》实验三实验报告
实验三实验报告 和张亚军同学一组: 实验三实验报告
- iframe-摘自网友
定义和用法 iframe 元素会创建包含另外一个文档的内联框架(即行内框架). HTML 与 XHTML 之间的差异 在 HTML 4.1 Strict DTD 和 XHTML 1.0 Strict ...
- struts2视频学习笔记 21(输入校验的流程)
课时21 输入校验的流程 1.类型转换器对请求参数执行类型转换,并把转换后的值赋给action中的属性. 2.如果在执行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,co ...
- Notepad++ 配置java编译环境
仅限于学习java或小的java程序使用.正常写代码还是eclipse吧 ---------------------分割线----------------------------- 1.配置JDK环境 ...
- linux apache 自动监护脚本
1 首先安装curl yum install curl 2 编写shell vi restart_apache.sh 写入一下内容 #!/bin/bashURL="http://127.0. ...
- strstr strchr strrchr
通过函数的定义来区分: 1.strstr: 返回子串出现的第一次位置 char *strstr(const char *haystack, const char *needle) 可见,strstr函 ...
- C# 正则表达式 验证:数字、带小数点数字、电话和手机
一.带小数点数字 public static bool IsNumber(string input) { string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\ ...
- [转载]Matrix类的使用
2013-12-18 11:31:00 转载自: http://www.cnblogs.com/mmy0925/archive/2013/01/22/2871009.html 在Android中,对图 ...
- 可以用WMI来获取磁盘及分区编号
{$APPTYPE CONSOLE} uses SysUtils, ActiveX, ComObj, Variants; function ListDrives : string; var FSWbe ...