Cow Exhibition (Deformed 01 backpack)

Link to the topic: http://poj.org/problem?id=2184
The meaning of the topic: there are n cows, si and fi are smart and interesting. We need to choose which cows to participate in the exhibition. Sum is the largest, while ensuring that these two values ​​are not negative.
Thinking This
question is a dp question, and its transfer equation is to see whether the cow is put down. Therefore, we can use the 01 backpack to solve this problem. The smartness is regarded as volume, and the fun is regarded as value. We need to calculate When the cleverness is greater than or equal to 0, the fun degree is the largest, and then the two are added together to make the addition the largest. However, one of the problems is that the cleverness as a volume will have a negative value, so we need to In the program, it is always positive to run. According to the value range of cleverness and fun, we can use the coordinate system to add 1,000 to the cleverness to make the range reach 0~2000; in this way, the cleverness is the volume The dp[] array will not have a negative volume. When we finalize it, we use 1000 as the starting point to determine it, which is equivalent to our original value (-1000 to 1000).
After moving the starting point, there are two options: one is that we can directly add 1000 to the smartness to perform calculations. In this case, in the transfer equation, dp will not appear as a negative volume, so We can carry out dynamic planning from back to front in a unified manner. But the trouble with this method is that we also need to define a separate array to store how many 1000s are added to each dp[], so as to subtract the 1000s added at the end, so there will be some trouble, the code will not show Up.
The second method is to move the starting point, but we do not directly add and subtract on the number, but when the smart value is negative, the update of our transfer equation is updated positively.
So this involves the need to ensure that the updated value does not affect the unupdated value in dynamic array planning.
Regarding forward and reverse updates.
When the value is positive, the code block:

for(int i=1;i<=n;i++)
	for(int j=maxx;j>=a[i];j--)
		dp[j]=max(dp[j],dp[j-a[i]]+b[i]);

When the value is positive, we update the dynamic array n times, that is, consider whether it will affect the i-th element. When we use the forward update, the updated value will not update us. If the value has an impact, you may not perceive it on a one-dimensional array. We might as well replace it with a two-dimensional dp[i][j]=max(dp[i-1][j],dp[i-1][ja [i]]+b[i]); can be understood by drawing a picture through this formula; in the
same way, when the value is negative, we can only use positive updates to maintain the independence of single-pass updates.
The complete code is as follows:

#include<bits/stdc++.h>
using namespace std;
#define inf 99999999
const int N=200005;
const int mid=100000;
int n;
int wight[105],value[105],dp[N];
int main()
{
    
    
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>wight[i]>>value[i];//智商属性体积,幽默感属性为价值,问题转换为求体积大等于0时的体积、价值总和。
    for(int i=0;i<=N;i++)//初始化dp,因为有负数,所以初始化为一个非常小的负数
        dp[i]=-inf;//一般memset只能赋值0和-1,所以对于0,-1以外的值一般直接用for循环
    dp[mid]=0;//起始点初始化为0
    for(int i=1;i<=n;i++)
    {
    
    //在w[i]正负的时候进行分类是为了保证每一种情况都不会影响为改变dp[]的独立性
        if(wight[i]>0)
        {
    
    
            for(int j=N-1;j>=wight[i];j--)//正数时候的背包
                dp[j]=max(dp[j],dp[j-wight[i]]+value[i]);
        }
        else
        {
    
    
            for(int j=0;j<N+wight[i];j++)//负数时候的背包
                dp[j]=max(dp[j],dp[j-wight[i]]+value[i]);
        }
    }
    int ans=0;
    for(int i=mid;i<=N;i++)//从智商和为0开始
    {
    
    
        if(dp[i]>=0)//情商和大于0
            ans=max(ans,dp[i]+i-mid);//dp数组存的是TF,通过i-mid计算TS
    }
    cout<<ans<<endl;
    return 0;
}

At the same time, in this problem, the memset function cannot be used to initialize dp, because the dp function we need to assign is not -99999999, which cannot be achieved through the memset function.

Guess you like

Origin blog.csdn.net/malloch/article/details/109012266