poj 2484 Cow Exhibition 【变形0-1背包】

题目:poj 2484 Cow Exhibition

题意:给出n头牛,每头牛有一个幸运值 si 和聪明值 ti ,现在要选出一些牛,让两个值的和最大,前提是sum(si)和sum(ti)都是非负值。

分析:此题数据量不大,可以暴搜+剪枝水过。

这里要说的是0-1背包的思想,这个题目明显的变形就是物品有两个属性值,而且都要选最大的。

那么我们可不可以把一个值固定下来来求另一个值的最大值,根据0-1背包的思想,定义状态:dp【i】表示装入一些物品使得sum(si)的时候最大的sum(ti)的值。

那么状态转移方程为dp【i+si】 = max(dp【i+si】,dp【i】+ti) ,正好表示一个物品的两种选择。

ac代码:

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 210000;
const int pos = 100000;
int dp[N];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(dp,-0x3f3f3f3f,sizeof(dp));
        dp[pos] = 0;
        int ma = pos,mi = pos;
        for(int i=0;i<n;i++)
        {
            int s,t;
            scanf("%d%d",&s,&t);

            if(s>0){
                for(int f=ma ;f>=mi;f--)
                    dp[f+s] = max(dp[f+s],dp[f]+t);
                ma+=s;
            }
            else{
                for(int f = mi;f<=ma;f++)
                    dp[f+s] = max(dp[f+s],dp[f]+t);
                mi+=s;
            }
        }
        int ans = 0;
        for(int i=pos;i<=ma;i++)
            if(dp[i]>=0)
                ans = max(ans,i-pos+dp[i]);
        printf("%d\n",ans);
    }
    return 0;
}

搜索超时代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 210000;
const int pos = 100000;
struct Node
{
    int s,t;
};
vector<Node> v;
int cmp(Node a,Node b)
{
    if(a.s!=b.s)
        return a.s>b.s;
    if(a.t!=b.t)
        return a.t>b.t;
}
int ans;
void dfs(int i,int sums,int sumt)
{
    if(i==v.size())
        return ;
    if(sums>=0 && sumt>=0)
        ans = max(ans,sums+sumt);
    if(sums<0 && v[i].s<=0)
        return ;
    dfs(i+1,sums,sumt);
    dfs(i+1,sums+v[i+1].s,sumt+v[i+1].t);
}
int main()
{
    int n;
    int sums = 0,sumt = 0;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            int s,t;
            scanf("%d%d",&s,&t);
            if(s>=0 && t>=0)
            {
                sums+=s,sumt+=t;
            }
            else if(s<=0 && t<=0)
                continue;
            else
                v.push_back((Node){s,t});
        }
        sort(v.begin(),v.end(),cmp);
        ans = 0;
        dfs(0,0,0);
        dfs(0,v[0].s,v[0].t);
        printf("%d\n",ans+sums+sumt);
    }
    return 0;
}
时间: 2024-11-09 00:51:51

poj 2484 Cow Exhibition 【变形0-1背包】的相关文章

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 (变形的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~100

POJ 1636 Prison rearrangement DFS+0/1背包

题目链接: POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 2194   Accepted: 984 Description In order to lower the risk of riots and escape attempts, the boards of two nearby prisons of equal

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(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背包,严重变形)

题意:有只奶牛要证明奶牛不笨,所以要带一些奶牛伙伴去证明自己.牛有智商和幽默感,两者可为负的(难在这),要求所有牛的智商和之 / 幽默感之和都不为负.求两者之和的最大值. 思路:每只牛可以带或不带上,是01背包问题.但是问题是没有明显的背包容量限制,却有了不为负的一些限制,相同的是要求最大和.必须找个背包容量出来. 1)背包容量:可以使用幽默感之和或智商之和作为背包容量.两者是提供的有明确范围的. 2)负号的问题:牛最多100只,而智商与幽默感最多为正负1千,那么 -1000*100<=x<=