poj2184 背包

 1 //Accepted    1492 KB    110 ms
 2 //背包
 3 //把si看成weight,Fi看成value,这可以表示成当dp[j]=max(dp[j-weight[i]]+value[i])
 4 //考虑到si可能为负,需要整段区间的平移
 5 //背包过程中,根据weight的正负,我们需要考虑dp的顺序,如果weight为正,这j从大到小
 6 //如果weight为负,则要从小到大,这是根据dp[i][j]=max(dp[i][j],dp[i-1][j-weight[i]]+value[i])
 7 //如果我们省略一维,这需要先更新j,再更新j-weight[i],当weight[i]为正时,我们要先更新大的。。。
 8 #include <cstdio>
 9 #include <cstring>
10 #include <iostream>
11 #include <queue>
12 #include <cmath>
13 #include <algorithm>
14 using namespace std;
15 /**
16   * This is a documentation comment block
17   * 如果有一天你坚持不下去了,就想想你为什么走到这儿!
18   * @authr songt
19   */
20 const int inf = 100000000;
21 const int imax_n = 105;
22 int value[imax_n];
23 int weight[imax_n];
24 int dp[200005];
25 int n;
26 int max(int a,int b)
27 {
28     return a>b?a:b;
29 }
30 void Dp()
31 {
32     for (int i=0;i<=n*2000;i++) dp[i]=-inf;
33     dp[n*1000]=0;
34     for (int i=1;i<=n;i++)
35     {
36         if (value[i]<=0 && weight[i]<=0) continue;
37         if (weight[i]>0)
38         {
39             for (int j=n*2000;j>=weight[i];j--)
40             if (dp[j-weight[i]]>-inf)
41             dp[j]=max(dp[j],dp[j-weight[i]]+value[i]);
42         }
43         else
44         {
45             for (int j=0;j<=n*2000+weight[i];j++)
46             if (dp[j-weight[i]]>-inf)
47             dp[j]=max(dp[j],dp[j-weight[i]]+value[i]);
48         }
49     }
50     int ans=0;
51     for (int i=n*1000;i<=n*2000;i++)
52     if (dp[i]>=0)
53     ans=max(ans,dp[i]+i-n*1000);
54     printf("%d\n",ans);
55 }
56 int main()
57 {
58     while (scanf("%d",&n)!=EOF)
59     {
60         for (int i=1;i<=n;i++)
61         scanf("%d%d",&weight[i],&value[i]);
62         Dp();
63     }
64     return 0;
65 }

时间: 2024-10-16 22:45:24

poj2184 背包的相关文章

POJ2184——背包DP——Cow Exhibition

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

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

POJ2184 01背包变形 xingxing在努力

这道题乍一看就有了思路,大体就是定了其中一个值然后再求另外一个值的最大值, 然而代码实现好坑, 题意是奶牛有两个属性 Ai和Bi, 让你求Ai和Bi和的最大值,注意Ai的和不能为负整数, Bi也一样..假设我们定了Ai我们来看下状态方程:f[i][j] = max(f[i-1][j], f[i-1][j-A[i]]+B[i]).当A[i]为正的时候就是我们经常遇到的01背包,使用滚动数组倒着排一遍, 然而当A[i]为负的时候我们就应该从小推到大,这样才对..代码如下: #include <cst

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 [POJ2184] [DP] [背包的负数处理]

题意: 有很多羊,每只羊有一个幽默度和智商,要选出一些羊,智商加幽默度总和最大,其中智商总和和幽默度总和都不能是负数. 样例输入: 5 -5 7 8 -6 6 -3 2 1 -8 -5 样例输出: 8 分析: 这很像一个01背包题,但是有两个价值,却没有重量 我们就把其中一个看成重量,另一个看成价值 答案就是max(i+dp[i]) 那如何保证数据大于0呢? 我们可以把价值总体向右移100000个单位,然后设原点O为100000 之后就可以进行dp了 转移方程仍是:dp[j]=dp[j-w[i]

POJ2184 Cow Exhibition 【01背包】

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9383   Accepted: 3601 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背包)

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

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

HDU 2189 悼念512汶川大地震遇难同胞——来生一起走(母函数或完全背包)

悼念512汶川大地震遇难同胞--来生一起走 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3773    Accepted Submission(s): 1913 Problem Description 妈妈你别哭泪光照亮不了我们的路让我们自己慢慢的走 妈妈我会记住你和爸爸的模样记住我们的约定来生一起走 上面这首诗节选自一位诗人纪念遇难