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. 在A页面刷新父框架中的B页面.(window.parent.?.location="")

    window.parent.leftFrame.location='left.jsp?menuid='+menu.id+'&menuname='+menu.title;

  2. jquery+css 实现即时变化颜色主题(通过input输入颜色值进行改变)

    实现效果需要自行导入jquery.js <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  3. solaris用户与文件权限管理

    此文章已于 20:45:28 2015/3/22 重新发布到 zhuxuekui3 solaris用户与文件权限管理1 类别    「网站分类」Oracle 一.用户与用户组管理 三种用户:超级用户. ...

  4. 串匹配算法讲解 -----BF、KMP算法

      参考文章: http://www.matrix67.com/blog/archives/115     KMP算法详解 http://blog.csdn.net/yaochunnian/artic ...

  5. shell(1):网络配置、BATH环境和通配符

    一.临时配置网络(ip,网关,dns) ifconfig查看网络配置 修改ip地址  ifconfig ens33 192.168.255.129/24 ens33网卡名称.192.168.255.1 ...

  6. HTML+CSS要点

    1.td占据多行 / 列时,其挤开的 td 不写(但是包裹 td 的 tr 要写) 2. display:td 的元素中的文本默认垂直不居中(table中的td中的文本是垂直居中的) 3.th虽然定义 ...

  7. Citrix_XenServer-6.1安装过程详解(转)

    本次为使用VirtualBox虚拟机过安装测试机过程,我们在使用Vm(无论是Vbox还是VMware等)我们的CPU都必须可支持Intel-V或AMD-V,并且在VM软件设置和BIOS设置开启虚拟化支 ...

  8. HTML5开发移动web应用—JQuery Mobile(1)

    JQuery Mobile是一个简单易用的web移动app开发框架.使用它就像使用jQuery一样,引入必要的文件就可以. 最基础的jQuery Mobile文件的结构代码例如以下: <body ...

  9. 命令+mybatis-generator插件自己主动生成Mapper映射文件

    学mybatis的时候,自己写各种 *Mapper.xml和 *Mapper.java,注意各种sql语句中的 id 是否匹配.xml中的namespace是否正确,非常麻烦有木有?今天博客内容就是高 ...

  10. EC2的维护更新

     2014年9月28日 近期几天.我们收到了一些客户关于我们即将进行维护更新的问题.下面是AWS全球Blog网站对这个问题的说明,供客户參照. 我们已经開始通知那些受影响的客户,关于我们即将实施的 ...