挑战程序设计竞赛2.3习题:Cow Exhibition POJ - 2184

"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.

本题难点一:智商与情商是每头奶牛的两种属性,就和东西的重量和价值一样,属于0-1背包问题。

本题难点二:智商情商可以是负数,但是数组下标不允许是负数,所以得加上bias,而这个bias就是总智商的最小值的绝对值即100头牛智商全为最小-1000,也就是bias = 100000,所以范围是0-200000(因为最大是100000,再加上bias就是)。但是情商可以不用加bias,因为dp的值可以是负数。

本题难点三:在一维dp数组情况下原来情商大于等于0是j从大到小遍历的,因为这样可以保证dp[i - 1][j - s[i]]的对应一位dp数组dp[j - s[i]]在dp[j]之后再修改,否则之前修改的话dp[j - s[i]]的值就对应dp[i][j - s[i]]而不是dp[i - 1][j - s[i]]的值,但是如果f[i]小于0,那么dp[i - 1][j - s[i]]就要在dp[i - 1][j]的右边也就是j - s[i] > j,因此如果还是j从大到小就会造成dp[i - 1][j - s[i]]的对应的值dp[j - s[i]]实际上对应dp[i][j - s[i]]的值,因为j - s[i] > j,所以j从大到小的话在dp[j]之前就已经修改好了dp[j - s[i]]的值,就会造成错误,所以此时应当从小到大遍历。综上所述当s[i] >= 0,从大到小,s[i] < 0,从小到大遍历j。

AC代码:

#include <stdio.h>
#include <algorithm>
using namespace std;
const int INF = 0x3fffffff;
int s[105], f[105];//s智商,f情商
int dp[200005];//从前i头奶牛中选取奶牛使得的智商为i - bias时,情商的最大值
int ans;
int main(void)
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d %d", &s[i], &f[i]);
    }
    fill(dp, dp + 200005, -INF);//初始化为不可达
    dp[100000] = 0;//实际上对应前0种,也就是初始状态下,智商,情商均为0,因为智商为0时加上bias就是100000
    for(int i = 0; i < n; i++)
    {
        if(s[i] < 0)
        {
            for(int j = 0; j - s[i] <= 200000; j++)
                if(dp[j - s[i]] != -INF)
                    dp[j] = max(dp[j], dp[j - s[i]] + f[i]);
        }
        else
        {
            for(int j = 200000; j >= s[i]; j--)
                if(dp[j - s[i]] != -INF)
                    dp[j] = max(dp[j], dp[j - s[i]] + f[i]);
        }
    }
    for(int i = 100000; i <= 200000; i++)//从智商为0开始遍历,也就是数组下标为bias开始
    {
        if(dp[i] >= 0)//如果智商可达到i且情商大于等于0
            ans = max(ans, dp[i] + i - 100000);
    }
    printf("%d\n", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/jacobfun/p/12240430.html

时间: 2024-11-07 23:15:19

挑战程序设计竞赛2.3习题:Cow Exhibition POJ - 2184的相关文章

挑战程序设计竞赛2.2习题:Allowance POJ - 3040

Allowance As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different denominations, where each denomination of coin evenly divides the

挑战程序设计竞赛2.2习题:Stall Reservations POJ - 3190

Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a

挑战程序设计竞赛2.3习题:Cheapest Palindrome POJ - 3280

Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag's contents are currently a

挑战程序设计竞赛2.4习题:Moo University - Financial Aid POJ - 2010

Bessie noted that although humans have many universities they can attend, cows have none. To remedy this problem, she and her fellow cows formed a new university called The University of Wisconsin-Farmside,"Moo U" for short. Not wishing to admit

挑战程序设计竞赛2.3习题:Making the Grade POJ - 3666

A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ would like. His cows do not mind climbing up or down a single slope, but they are not fond of an alternating succession of hills and valleys. FJ would like t

挑战程序设计竞赛2.6习题:X-factor Chains POJ - 3421

Given a positive integer X, an X-factor chain of length m is a sequence of integers, 1 = X0, X1, X2, …, Xm = X satisfying Xi < Xi+1 and Xi | Xi+1 where a | b means a perfectly divides into b. Now we are interested in the maximum length of X-factor ch

挑战程序设计竞赛3.1习题:Moo University - Financial Aid POJ - 2010

(原题见POJ2010) 这道题我之前采用了优先队列+预处理的方法求解(https://www.cnblogs.com/jacobfun/p/12244509.html),现在用二分的办法进行求解. 一开始我很纳闷,采用二分求解本题,如果二分的mid值不符合条件,按照二分右边界应该为mid - 1(我采用前闭后闭的二分),那么如果mid + xxx(xxx大于0)可以呢?(考虑mid不行是因为左边最小加起来大了,mid ~ mid + xxx中有极小值,使得mid + xxx的左边可以满足,那么

挑战程序设计竞赛3.2习题:Bound Found POJ - 2566

Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: "But I want to use feet, not meters!"). Each signal seems to come in two

挑战程序设计竞赛2.3:Wooden Sticks POJ - 1065

There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing