[2020.10.28 SSL Simulation Tournament T2] [Luogu P2340] SMRTFUN [01 Backpack of Variation]

Topic background

Title
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. Since 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 ≤ 100

• The second line to the N + 1 line: There are two integers in the i + 1 line: Si and Fi, which represent 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

analysis:

Since for each cow we can only choose or not, it can be converted into a 01 010 1 BackpackQuestions
Regarding EQ asvalue,IQ asvolumecan be
dp [i] [j] dp[i][j]d p [ i ] [ j ] is the formeriii cow as IQ isjjThemaximum EQ value at j .
Note that the IQ value can benegative, but oursubscriptcannot be negative, so we need toshift thewholeback by1000 ∗ 100 1000*10010001 0 0 bit
Because it is to find the maximumIQ EQ andfinally fordp [n] [i] + i dp[n][i]+idp[n][i]+i takesthe maximum valuecan be

CODE:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define reg register 
using namespace std;
const int N=100005;
const int LIMIT=100000;
int n,s[105],f[105],dp[105][N],ans;
int main(){
    
    
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&s[i],&f[i]);
	memset(dp,-0x3f,sizeof(dp));
	dp[0][0]=0;dp[0][LIMIT]=0;
	for(reg int i=1;i<=n;i++)
		for(reg int j=2*LIMIT;j>0;j--)  //从-1000*100~1000*100 -> 0~2000*100 
			dp[i][j]=max(dp[i-1][j],dp[i-1][j-s[i]]+f[i]);  //取或不取
	for(int i=LIMIT;i<=2*LIMIT;i++)  //在这个范围取最大 
		if(dp[n][i]>=0)
			ans=max(ans,dp[n][i]+i-LIMIT);  //记得原本值要减 100*1000 
	printf("%d",ans);		
	
	return 0;
}

Guess you like

Origin blog.csdn.net/dgssl_xhy/article/details/109340304