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 <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
const int inf = 0x3f3f3f3f;
int M = 100000;
int f[200000 + 10];
int A[100 + 10], B[100 + 10]; 

int main()
{
    int n;
    while(scanf("%d", &n) == 1)
    {
        for(int i=1; i<=n; i++)
            scanf("%d%d", &A[i], &B[i]);
        for(int i=0; i<=2*M; i++)
            f[i] = -inf;
        f[M] = 0;
        for(int i=1; i<=n; i++)
        {
            if(A[i] >= 0)
            {
                for(int j=2*M; j>=A[i]; j--)
                    f[j] = max(f[j], f[j-A[i]]+B[i]);
            }
            else
            {
                for(int j=0; j<=2*M+A[i]; j++)
                    f[j] = max(f[j], f[j-A[i]]+B[i]);
            }
        }
        int res = 0;
        for(int i=M; i<=2*M; i++)
            if(f[i] >= 0)
                res = max(res, i-M+f[i]);
        printf("%d\n", res);
    }
    return 0;
}
时间: 2024-08-23 12:16:14

POJ2184 01背包变形 xingxing在努力的相关文章

Uva624 01背包输出方案 xingxing在努力

题目是给你一个长度为N的磁带, 让你将t首歌放入磁带中, 输出歌曲总长度不超过N的情况下的方案. 代码如下: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int c[30]; int f[10000 + 100]; int vis[30][10000 + 10]; int n, m; int main() { while(scanf("%d%d&q

HDU 2955 Robberies --01背包变形

这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即转化为01背包问题. 此时dp[v]表示抢劫到v块钱成功逃跑的概率,概率相乘. 最后从大到小枚举v,找出概率大于逃跑概率的最大v值,即为最大抢劫的金额. 代码: #include <iostream> #include <cstdio> #include <cstring>

codeforce Gym 101102A Coins (01背包变形)

01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define MAXW 15005 #define N 155 #define LL long long #define MOD 1000000007 int w1[N],w2[N]; LL dp1[MAXW],dp2[MAXW]; int main(

Wikioi 1025 01背包变形

这题多加了菜品必选编号,所以刚开始不知道怎么写,原来就把必选的处理下就行了,因为有重复,但是相同的价值与价格都一样,所以这里就直接挑出来就行了. 把不是必选的在里面用dp即可,dp之前也要把重复的舍去. 因为总价格容量为浮点数,所以先乘以10变成整数就可以用01背包了. #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <deque&

codeforces 742D Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses ——(01背包变形)

题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <vector> 5 using namespace std; 6 const int N = 1000 + 5;

POJ 3093 Margaritas on the River Walk (0-1背包变形)

这题目的思路很巧妙,什么情况下剩下的所有物品都放不下呢?就是当前剩余物品中最小的那个也放不下.所以,先把物品按照容量从小到大排序,依次枚举当前背包为放不下的最小物品的情况. 对于当前物品i,必有1到i-1的所有物品都放进去,这时候比i大的物品谁放谁不放是不确定的.转换成0-1背包问题:把前i-1个物品都放进去以后,得到空间为tsum - sum[i-1](前缀和)的包,只要从第i+1到第n个物品中拿出一个方案填充这个包使得剩余体积小于第i个物品的体积就可以了,把总方案数累加就是结果! 注意特殊情

HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4739    Accepted Submission(s): 2470 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

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

soj 2222 01背包变形

背景:wa~Tl~看来背包还是很欠缺啊~ 思路1(1500ms):变形的01背包.把题目改为:选择一组HP和大于等于所需血量且这组物品的分数之和最小,即可.这里把HP看做cost,score看做weight.但是这个题有个特点是最后选择HP必须大于等于k,容易想到,最多我们会有k+10000(10000为单个物品的最大HP值)的HP值,所以我们有这样的转移方程: for i 0....n for j K+10000....cost[i] F[j]=min(F[j],F[j-cost[i]]+we