台州 OJ 5072 Cow Exhibition 01背包

给出 n 头牛,每头牛有两个属性 smartness 和 funness ,求从所有的牛里选一些牛,使这些牛的 smartness + funness 的和最大,且 smartness 的和、funness的和都要大于零。

定义 dp[i][j] 表示前 i 头牛在 smartness 为 j 时 funness 的最大值。

dp[i][j+s[i]] = max(dp[i-1][j+s[i]], dp[i-1][j] + f[i])    s[i]、f[i] 分别表示第 i 头牛的 smartness 和 funness。

因为 smartness 可能为负,要保证在 dp 时下标要大于等于零,所以在 dp 的过程中将 j 加一个数,最后求答案时再转化回原来的数。

代码(用了两个一维数组滚动优化):

#include <iostream>
#include <cstring>
using namespace std;

const int MAX = 2005;
const int INF = 0x3f3f3f3f;
const int zero = 100000; 

int n;
int now[MAX*100], pre[MAX*100];
int s[MAX], f[MAX];

int main(){
    freopen("input.txt", "r", stdin);
    int ans = 0, minS = 0, maxS = 0;
    cin >> n;
    for(int i=1; i<=n; i++){
        cin >> s[i] >> f[i];
        if(s[i] >= 0 && f[i] >= 0){        //smartness 和 funness 都大于 0 的牛一定选
            ans += s[i] + f[i];
            i--; n--;
        }else if(s[i] < 0 && f[i] < 0){        //都小于 0 的一定不选
            i--; n--;
        }
    }

    //初始化
    memset(now, -INF, sizeof(now));
    now[zero] = 0;
    memcpy(pre, now, sizeof(now));

    //DP
    minS = maxS = zero;        // 0 变成 100000,将 smartness 为负数的情况转化为全部是正数
    for(int i=1; i<=n; i++){
        for(int j=minS; j<=maxS; j++){
            now[j+s[i]] = max(pre[j+s[i]], pre[j] + f[i]);    //smartness j+s[i] 时,funness 的最大值,考虑第 i 头牛放不放
        }
        minS = min(minS + s[i], minS);
        maxS = max(maxS + s[i], maxS);
        memcpy(pre, now, sizeof(now));
    }

    int maxn = 0;
    for(int i=zero; i<=maxS; i++){
        if(now[i] >= 0){
            maxn = max(maxn, (i-zero) + now[i]);    //要 - zero 转化为原来的 smartness
        }
    }

    cout << ans + maxn;

    return 0;
}
时间: 2024-08-02 11:31:51

台州 OJ 5072 Cow Exhibition 01背包的相关文章

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背包变形

点击打开链接链接 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

PKU 2184 Cow Exhibition 01背包

题意: 有一些牛,每头牛有一个Si值,一个Fi值,选出一些牛,使得max( sum(Si+Fi) ) 并且 sum(Si)>=0, sum(Fi)>=0 思路: 随便选一维做容量(比如Fi),另一维做价值,然后直接做01背包. 做的时候注意一下方向. 最后,在合法解里面找一下最优解就好了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib>

POJ 2184 Cow Exhibition 01背包变型 好题

题意:n头牛,每头牛有两个属性值,smart值s,fun值f,求选出其中的m头牛,使得s和f的总和最大,并保证各自的s和以及f和都大于0. 分析: 怎么保证TS和TF都是>=0的条件下使得TS+TF最大? 先保证TS和TF都是>=0,然后遍历一边取TS+TF的最大值就ok了. 转化问题,求s和为某个固定值时候最大的f和值,然后遍历这些所有的s和以及对应的f和值,求出总和总和最大的那个. 那么这样就是一个0-1背包问题,可以把s值理解为代价,f值理解为价值 dp[c]代表s和为c时候,f和能取到

poj2184 Cow Exhibition p-01背包的灵活运用

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=2184 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 ar

Cow Exhibition 变种背包

Cow Exhibition Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status 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 t

背包 [POJ 2184 SWUST OJ 145] Cow Exhibition

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

POJ2184---Cow Exhibition(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 exhibition