[2020.10.28 SSL Universal Simulation Tournament T2] [SMRTFUN] [DP01 backpack variant]

Topic background

Luo Gu's original question
Cows want to prove that they are smart and funny. For this reason, Bessie has prepared a dairy cow expo. She has interviewed N cows and determined the IQ and EQ of each cow.

Title description

Bessie has the right to choose which cows to participate in the exhibition. Because negative IQ or EQ can cause negative effects, Bessie does not want the sum of IQ of the cows to be less than zero, or the sum of EQ less than zero. If these two conditions are met, she hopes that the greater the sum of the IQ and EQ of the cows on display, the better. Please help Bessie to find this maximum.

Input format

• The first line: a single integer N, 1 ≤ N ≤ 400

• The second line to the N + 1 line: There are two integers in the i + 1 line: Si and Fi, representing the IQ and EQ of the i-th cow, −1000 ≤ Si; Fi ≤ 1000

Output format

• A single integer: represents the maximum sum of EQ and IQ. Bessie can not let any cows participate in the exhibition, if this is the best way, output 0

Sample input and output

Enter #1

5
-5 7
8 -6
6 -3
2 1
-8 -5

Output #1

8

Instructions/tips

Choose the first cow, the third cow, and the fourth cow. The sum of IQ is −5+6+2 = 3, and the sum of EQ is 7−3+1 = 5. Adding the second cow can increase the sum to 10, but since the emotional intelligence and the sum become negative, it is not allowed.

analysis

This question can be regarded as a 01 backpack, for each cow, there are two options or not.
First consider how to represent the state.
dp[j] The first i cows are completed with for.

State transition equation?
dp [j] = max (dp [j], dp [j − s [i]] + f [i]) dp[j] = max(dp[j], dp[j−s[i]] + f [i])dp[j]=max(dp[j],dp[js[i]]+f[i])

Initial state:

memset(dp,0x3f, sizeof(dp)); //负无穷
dp[400000] = 0;

Upload code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

int n,s[1001],f[1001],d[500001],ans;

int main()
{
    
    
	cin>>n;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>s[i]>>f[i];
	} 
	memset(d,-0x3f,sizeof(d));
	d[100000]=0;
	for(int i=1;i<=n;i++)
	{
    
    
		if(s[i]>=0)
		{
    
    
			for(int j=2*100000;j>=s[i];j--)
			{
    
    
				d[j]=max(d[j],d[j-s[i]]+f[i]);
			}
		}
		else
		{
    
    
			for(int j=0;j<=2*100000+s[i];j++)
			{
    
    
				d[j]=max(d[j],d[j-s[i]]+f[i]);
			}
		}
	}
	for(int i=100000;i<=2*100000;i++)
	{
    
    
		if(d[i]>0)
		{
    
    
			ans=max(ans,i+d[i]-100000);
		}
	}
	cout<<ans;
	return 0;
} 

Guess you like

Origin blog.csdn.net/dglyr/article/details/109345777