Knapsack problem with a negative value poj2184

Source: Internet
Author: User


1: Select the cow to ensure that two attributes and both are greater than 0.



2. Maximum sum.



Think of anything. 01 Backpacks, one count either not, or use, to make the most value.



Because a negative value, and two values are greater than 0, then assume that the first property is already greater than 0--reset 0 points, let 100*1000 0 points, over this point, the selected first attribute and greater than 0, and vice versa. Then you just need to take care of the second type of attribute. The first is counted as consumption, and the second attribute is the value (equivalent to spending a to get B.) Because a has already been shown in the coordinates. Less than 100000 is negative, otherwise positive), DP. Then you need to see if the DP value is greater than 0. 01 Backpack One-dimensional DP is reversed. But when the a[i] is negative, if it is still reversed, instead of the recursive style is not to trot, because it is DP "J-a[i", a "I" is negative, j-a instead becomes larger.


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define nl n<<1
#define nr (n<<1)|1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const ll INFF=0x3f3f3f3f3f3f3f3f;
const double pi=acos(-1.0);
const double eps=1e-9;
const ll mod=1e9+7;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0' | ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void Out(int aa)
{
    if(aa>9)
        Out(aa/10);
    putchar(aa%10+'0');
}
int a[105],b[105];
int dp[200010];
int main()
{
    int n=read();
    for(int i=1;i<=n;i++)scanf("%d%d",&a[i],&b[i]);
    memset(dp,-INF,sizeof(dp));
    dp[100000]=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]>0)
        {
            for(int j=200005;j>=a[i];j--)
                dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
        }
        else
        {
            for(int j=0;j<=200005+a[i];j++)
                dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
        }
    }
    int ans=0;
    for(int i=100000;i<=200005;i++)
    {
        if(dp[i]>0)
            ans=max(ans,dp[i]+i-100000);
    }
    cout<<ans<<endl;
    return 0;
}

So that's when the formula starts running from 0. Specific look at the code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.