Cow exhibiton (01背包变形)

"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

Hint

OUTPUT DETAILS:

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.

这道题的关键有两点:一是将smartness看作花费、将funness看作价值,从而转化为01背包;二是对负值的处理,引入一个shift来表示“0”,这里的shift一定要大于每一个smartness的绝对值,另外在遍历cost[]的时候如果cost[i]>0,显然时从开的数组的最大值maxm 开始往下减,如果cost[i]<0,则是从0(是0,不是shift)开始往上增加。大于0的情况容易想到,小于0的情况比较费解,需要仔细思考。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 201005;
const int inf = 99999;
int d[maxn];
int main()
{
    int n,a,b;
    while(cin>>n){
        for(int i=0;i<maxn;i++)
            d[i]=-inf;
        d[100000]=0;
        for(int i=0;i<n;i++){
            cin>>a>>b;
            if(a<0&&b<0)
                continue;
            else if(a>0)
                for(int k=200000;k>=a;k--)
                    d[k]=max(d[k],d[k-a]+b);
            else
                for(int k=a;k<=200000;k++)
                    d[k]=max(d[k],d[k-a]+b);
        }
        int maxd = -inf;
        for(int i=200000;i>=100000;i--){
            if(d[i]>=0)
                maxd = max(maxd,d[i]+i-100000);
        }
        if(maxd<0)
            cout<<"0"<<endl;
        else
            cout<<maxd<<endl;
    }
}

  

时间: 2024-10-12 19:29:06

Cow exhibiton (01背包变形)的相关文章

poj 2184 Cow Exhibition 01背包变形

点击打开链接链接 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9288   Accepted: 3551 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 p

HDU 2955 Robberies --01背包变形

这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即转化为01背包问题. 此时dp[v]表示抢劫到v块钱成功逃跑的概率,概率相乘. 最后从大到小枚举v,找出概率大于逃跑概率的最大v值,即为最大抢劫的金额. 代码: #include <iostream> #include <cstdio> #include <cstring>

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

01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define MAXW 15005 #define N 155 #define LL long long #define MOD 1000000007 int w1[N],w2[N]; LL dp1[MAXW],dp2[MAXW]; int main(

Wikioi 1025 01背包变形

这题多加了菜品必选编号,所以刚开始不知道怎么写,原来就把必选的处理下就行了,因为有重复,但是相同的价值与价格都一样,所以这里就直接挑出来就行了. 把不是必选的在里面用dp即可,dp之前也要把重复的舍去. 因为总价格容量为浮点数,所以先乘以10变成整数就可以用01背包了. #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <deque&

codeforces 742D Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses ——(01背包变形)

题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <vector> 5 using namespace std; 6 const int N = 1000 + 5;

POJ 3093 Margaritas on the River Walk (0-1背包变形)

这题目的思路很巧妙,什么情况下剩下的所有物品都放不下呢?就是当前剩余物品中最小的那个也放不下.所以,先把物品按照容量从小到大排序,依次枚举当前背包为放不下的最小物品的情况. 对于当前物品i,必有1到i-1的所有物品都放进去,这时候比i大的物品谁放谁不放是不确定的.转换成0-1背包问题:把前i-1个物品都放进去以后,得到空间为tsum - sum[i-1](前缀和)的包,只要从第i+1到第n个物品中拿出一个方案填充这个包使得剩余体积小于第i个物品的体积就可以了,把总方案数累加就是结果! 注意特殊情

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

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4739    Accepted Submission(s): 2470 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

poj 2184 Cow Exhibition(dp之01背包变形)

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 exhibitio

poj(2184)——Cow Exhibition(01背包变形)

其实我想说这道题我觉得我自己并没有深刻的理解.但是今天做了一下,先把现在的想法记录下来 . 题目的大致意思是: 有N头牛,每头牛都有一个s值代表智商值,f值代表它的幽默值. 然后问你智商值和幽默值的总和值最大是多少,其中必须保证智商值的和与幽默值的和为非负数. 一开始我想到的也是01背包,但是这里还有负值,于是我就没有办法了.于是学习到了一个相当于把坐标平移的方法. 因为这里有-1000到0的值,于是我们把它们全都移到右边去,于是变成了非负值0-2000. 解法: 01背包. 但是要注意的是当x