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

链接:poj 2184

题意:给定n头牛,每头牛的的智商(si)和幽默感(fi)已知,求在保证智商(S)的和及幽默感(F)的和都为非负的情况下,智商和幽默感(S+T)的最大值

分析:题的本质即从n头牛中选出S>=0&&T>=0时,S+T的最大值

以智商最为容量,幽默感作为价值,因为每头牛只能选一次,就转化01背包了,

dp[i]为智商为i时幽默感的最大值,则状态转移方程为 dp[j]=max(dp[j],dp[j-s[i]]+f[i]);

但是智商总和范围-100000~100000可能为负,而数组下标不能为负,可以将下标整体加100000,存到0~200000中

那么平衡点(即0)变为100000

#include<stdio.h>
#define MIN -999999
int dp[200010];
int main()
{
    int n,i,j,s[105],f[105],max;
    while(scanf("%d",&n)!=EOF){
        for(i=0;i<=200000;i++)
            dp[i]=MIN;
        dp[100000]=0;
        for(i=1;i<=n;i++)
            scanf("%d%d",&s[i],&f[i]);
        for(i=1;i<=n;i++){
            if(s[i]<0&&f[i]<0)        //智商和幽默感都为负,肯定不能被选
                continue;
            if(s[i]>0)
                for(j=200000;j>=s[i];j--){      //s[i]>0逆序循环
                    if(dp[j-s[i]]+f[i]>dp[j])
                        dp[j]=dp[j-s[i]]+f[i];
                }
            else
                for(j=s[i];j<=200000+s[i];j++)  //s[i]<0顺序循环
                    if(dp[j-s[i]]+f[i]>dp[j])
                        dp[j]=dp[j-s[i]]+f[i];
        }
        max=MIN;
        for(i=100000;i<=200000;i++)      //求幽默感和智商都为正的总和最大值
            if(dp[i]>=0&&dp[i]+i-100000>max)
                max=dp[i]+i-100000;      //因为下标都加了100000,所有要减
        if(max==MIN)
            max=0;
        printf("%d\n",max);
    }
    return 0;
}

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

时间: 2024-08-05 03:24:54

poj 2184 Cow Exhibition (变形的01背包)的相关文章

poj 2184 Cow Exhibition 【另类01背包】

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9424   Accepted: 3619 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 t

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

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

poj 2184 Cow Exhibition(01背包)

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 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

poj 2184 - Cow Exhibition (01背包) 解题报告

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10279   Accepted: 4016 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

POJ 2184 Cow Exhibition (01背包)

题意:每行给出si和fi,代表牛的两个属性,然后要求选出几头牛,是的则求出总S与总F的和,注意S与F都不能为负数 析:用dp[i]来表示存放s[i]的时最大的f[i],其实就是一个01背包.只是取不取的关系.注意是有负数,所以把数组开大一点,然后s[i]的正负数, 我们取的顺序不同,正数是逆向,负数是正向,要不然可能有重复.还不知道为什么交G++就RE,交C++就能过. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo

POJ 2184 Cow Exhibition (变种01背包)

题意:有一些奶牛,他们有一定的s值和f值,这些值有正有负,最后让保证s的和为非负且f的和为非负的情况下,s+f的最大值. 思路:很明显的就是取与不取的问题,对于这类问题的第一想法就是背包,但是这道题目很明显与一般的背包不同,因为有负数,但是联想到以前也有这种将负数存入下标的情况,那就是将数组开大,换一种存法 我们用dp[i]存放每个s[i]能得到的最佳F,那么我们就可以根据s[i]的取值采取两种不同的01背包取法,在取完之后,然后再根据背包的有无再去求得最佳答案即可 #include <stdi

POJ 2184 Cow Exhibition

01背包.把一个属性当成费用,另一个属性当成价值.费用可能为负数,处理的时候加上100000就可以了. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int INF=2100000000; const int maxn=100+10; int n; int a[maxn],b[maxn]; int F=1000