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. G 全然背包

    <span style="color:#3333ff;">/* /* _________________________________________________ ...

  2. 关于C语言中二维数组传參————————【Badboy】

    直接上代码: #include void Fun(int *a[],int m,int n)// { printf("%d\t",*a);//[0][0] /* int e[2][ ...

  3. Odoo multiprocessing

    Odoo 在 非 windows 系统下, 支持 并行处理,开启 workers 配置项 即可.     odoo有以下配置项 跟 并行处理有关     配置项 帮助信息 解说 limit_memor ...

  4. python(28)- 面向对象练习Ⅱ

    题目一:总结 1.什么是绑定到对象的方法,如何定义,如何调用,给谁用?有什么特性? 类内定义的函数,不经装饰器装饰,被实例化对象调用,会默认传入一个self参数,对象将会传递给self: 定义方式比较 ...

  5. iOS----FMDB---看这个可以解决大部分你遇到的问题

    SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库. iOS SDK很早就支持了SQLite,在使用时,只需要加入 libsqlite3.dyl ...

  6. linux系列之-—04 自动删除n天前日志【转】

    让Linux系统定时清理一些不需要的文件,日志很有必要 1. 删除文件命令: find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \; ...

  7. 照猫画虎学gnuplot之折线图

    本节重点:怎样利用已知数据来画折线图. 首先说明:gunplot文件的后缀名为*.plt.本节讲述怎样利用已知数据来画折线图,顾名思义必定涉及到两个文件:一个是须要的数据文件,即*.dat文件.还有一 ...

  8. oracle [union.minus.intersect]

    union 两张表的相同字段的数据[记录类型和列数要一致],合并,并且去重 can replace with "in" (但是如果是两个不同的表而且没什么关联的话必须要union了 ...

  9. css-装饰

    css -在标签上设置style样式 background-color:#2356a1 height:48px -编写样式方法 1.标签的style属性 2.鞋子head里面,style标签中编写 - ...

  10. Canvas学习笔记——动画中的三角学

    示例1,跟随鼠标的键头:   需要掌握一个重要的公式,这个方法返回从 x 轴到点 (x,y) 之间的角度 Math.atan2(dy,dx); 关键代码: function Arrow() { thi ...