Cow Exhibition (01 背包变形)

Cow Exhibition

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem 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
 

Source
PKU
 
题目大意:每个奶牛有一个s值和一个f值,现在要求选出一些奶牛他们的s值和f值的和最大,且他们的f值的和,s值的和都不能小于0 

感觉是个二维背包,但是数组开不了那么大啊,但是每一组的数据都是相关联的,所以可以一个当费用,一个当价值,然后最后相加求和。

有一个小优化(s[i]  f[i]都是负数就可以continue了。      因为考虑到求和 并且是 01背包的问题,所以这个背包初始值一定是-INF

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 100000;
const int INF = 0x3fffffff;
int dp[200005];
int s[105], f[105];
int main() {
        //freopen("in.txt", "r", stdin);
    int n;
    while (cin >> n) {
        for (int i = 0; i < n; ++i) {
            cin >> s[i] >> f[i];
        }
        for (int i = 0; i <= 2 * maxn; ++i) {
            dp[i] = -INF;
        }
        dp[maxn] = 0;
        for (int i = 0; i < n; ++i) {
            if (s[i] > 0 || f[i] > 0) {
                if (s[i] > 0) {
                    for (int j = 2 * maxn; j >= s[i]; --j) {
                        if (dp[j - s[i]] > -INF) {
                            dp[j] = max(dp[j], dp[j - s[i]] + f[i]);
                        }
                    }
                } else {
                    int p = 2 * maxn + s[i];
                    for (int j = 0; j <= p; ++j) {
                        if (dp[j - s[i]] > -INF) {
                            dp[j] = max(dp[j], dp[j - s[i]] + f[i]);
                        }
                    }
                }
            }
        }
        int ans = 0;
        for (int i = maxn; i < (maxn * 2); ++i) {
            if (dp[i] > 0) {
                ans = max(ans, dp[i] + i - maxn);
            }
        }
        cout << ans << endl;
    }
}


你可能感兴趣的:(背包问题)