poj2184Cow Exhibition【01背包 负数】

来源:互联网 发布:手机android编程软件 编辑:程序博客网 时间:2024/05/16 13:54

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 78 -66 -32 1-8 -5

Sample Output

8

磨叽了好几个小时的01背包终于在改了n多地方之后A掉了

其实题意不难 而且作为地信的小孩儿 由“西移500千米”这种方法中也应该能想到体积为负数数组表示不了这根本就不是事!做题之前不小心瞅了一眼题解,说是把聪明值当做体积,有趣值当做价值orz依旧不会做 翻了好多题解 其一不理解为啥有趣值为负数时是完全背包??其二不懂为啥说好了+1000数组下标里并没有显示出来??

直到我看到了:点击打开链接 这篇 完美的避开了我之前的疑惑 ~ 递推的形式依旧是01背包的模板 只不过多了一个用来储存当前体积所多加的1000的个数

/*********poj21842015.10.25*********/#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <iostream>#include <algorithm>#include <map>#define LL long longusing namespace std;const int N = 110;const int M = 200010;const int NN = 1000;const int INF = 10000000;int w[N],v[N],sum;int dp[M], cnt[M];int main(){   // freopen("1.txt","r",stdin);    int n;    while(~scanf("%d",&n))    {        sum=0;        for(int i=1;i<=n;i++) {            scanf("%d%d",&v[i],&w[i]);            if(v[i]<0&&w[i]<0){i--,n--;continue;}            v[i]+=1000;            sum+=v[i];        }        memset(dp,-0x3f3f3f3f,sizeof(dp));        dp[0]=0;        memset(cnt,0,sizeof(cnt));        for(int i=1;i<=n;i++)        {            for(int j=sum;j>=v[i];j--)            {                if(dp[j-v[i]]+w[i]-(cnt[j-v[i]]+1)*1000>dp[j]-cnt[j]*1000)                {                    cnt[j]=cnt[j-v[i]]+1;                    dp[j]=dp[j-v[i]]+w[i];                }            }        }        int ans=0;        for(int i=0;i<=sum;i++)        {            if(i-cnt[i]*1000>0&&dp[i]>0)//            {                ans=max(ans,dp[i]-cnt[i]*1000+i);            }        }        printf("%d\n",ans);    }    return 0;}
P.S 最近状态又不好了 这么一个小破题从9点多做到现在 还一堆错误 →_→ 看到别人这几天去比赛成绩还那么好亚历山大啊,觉得自己随时都有可能被15届挤出实验,挤不出实验室明年也未必拿什么奖,整天看到自己比自己专业的强不少,内心极度膨胀;看到自己跟14、15届的差好多,心里又极度恐慌。每天就这样徘徊在自负与自卑中郁郁寡欢T^T 当然也不排除是被毛概那个奇葩老师搞的 orz 不就4分么 老子不惧你 !专业课不也就这么回事吗 ,吓唬谁呢。凭良心讲 ,你讲课是不错,这就能成唯一飞扬跋扈的资本?

不说她了,想想明年这个时候大局已定,有时间矫情还不如多学点。每次都是这么告诉自己的 ,可是每次都做不到。你口口声声说喜欢编程,怎么没见着你废寝忘食的打代码,难道你选择本科就业仅仅是对考研的逃避?你自我安慰说周末回家需要休息,你真的需要那么多的休息时间吗?明明知道自己休息一下状态半天回不来,明明知道自己没学习的那么多,明明知道自己根本没有一点嘚瑟的资本,可是依旧一遍一遍刷着网页,依旧自以为是!亲爱的,这样的状态什么时候到头?非要等到明年这个时候一个offer都没拿到才开始后悔自己本科的碌碌无为?非要等到明年15届进来把自己挤出实验室才想起来刷题?你说你开学前两周的时候忙着跑缓考成绩,再过两三周忙着招新,现在可算是闲下来了,然后就有时间开始像7月份一样瞎矫情了?!

亲爱的周小姐,你是要跟屋子里的这些人竞争,并不是要把目标定在和本专业的那些人一样啊。互联网的冬天要来了,继阿里拥抱变化之后,度厂也不甘落后,大多数的公司都在缩照,你有什么筹码一直笑到明年的这个时候?我是爱编程的,爱算法的……

0 0