Happy Jinming backpack

C. Happy Jin Ming (happy) [Discussion]
Description
Jin Ming is very happy today. The new house he bought at home is about to receive the key. There is a spacious room dedicated to him in the new house. What makes him even more happy is that his mother said to him yesterday: "What items do you need to buy in your room and how to decorate it, you have the final say, as long as it does not exceed N yuan." Jin Ming started to make a budget early this morning, but he wanted to buy too many things, which would definitely exceed the N yuan limit by his mother. Therefore, he specified an importance degree for each item, divided into 5 levels: represented by an integer 1 to 5, and the fifth level is the most important. He also checked the price of each item (all in integers) from the Internet. He hopes to maximize the sum of the product of the price and importance of each item on the premise that it does not exceed N yuan (which can be equal to N yuan).
Suppose the price of the j-th item is v[j] and the importance is w[j]. A total of k items are selected, and the numbers are j1, j2, ……, jk, then the total sum is:
v[j1 ]∗w[j1]+v[j2]∗w[j2]+…+v[jk]∗w[jk]. (Where * is the multiplication sign)
Please help Jinming design a shopping list that meets the requirements.

Input
input line of the first file happy.in, two positive integers, separated by a space:
Nm
(where N (<30000) represents the total amount of money, m (<25) to a desired number of items purchased.)
From line 2 to line m+1, line j gives the basic data of the item numbered j−1, and each line has 2 non-negative integers.
vp
(where v indicates the price of the item (v≤10000), p indicates the importance of the item (1~5))

The output
output file happy.out has only one positive integer, which is the maximum value of the product of the price and importance of items that do not exceed the total amount (<100000000).

Samples
Input Copy
1000 5
800 2
400 5
300 5
400 3
200 2
Output
3900

Idea
Money-------》weight

Importance----》Value

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
priority_queue <int,vector<int>,less<int> > q;
priority_queue <int,vector<int>,greater<int> > pp;
vector <int> aa;
typedef struct sty{
    
    
                   int str;
                    int end;
                }ss;
int cmp(ss a,ss b)
{
    
    
       if(a.str==b.str)
        return a.end<b.end;

        return a.str<b.str;
    }
int pan(int n)
{
    
    
       for(int i=2;i<=sqrt(n);i++)
            if(n%i==0)
            return 0;

        return 1;

    }
int pan1(int n)
{
    
    
       if(n%10+n/10%10==n/100%10+n/1000)
        return 1;
    return 0;
    }
int max1(int x,int y)
{
    
    
            if(x>y)
            return x;
        return y;
    }
int min1(int x,int y)
{
    
    
            if(x>y)
                return y;
            return x;
        }
const int maxn=1e6+888;
const int N=1006;
ll n,m,sum=0,minx=-1;
int a[maxn];

int pre[maxn],sz[maxn],rk[maxn];
int c[maxn];

void f(ll *p)
{
    
    
    *p/=2;
}
void ff(ll *p)
{
    
    
    *p=3*(*p)+1;
}

int main(){
    
    

 int x;
 int z,t;
 cin>>z>>t;
 for(int i=0;i<t;i++)
 {
    
    
     cin>>a[i];
     cin>>x;
     c[i]=a[i]*x;
 }
 for(int i=0;i<t;i++)
 {
    
    
     for(int j=z;j>=a[i];j--)
     {
    
    
         pre[j]=max(pre[j],pre[j-a[i]]+c[i]);
     }
 }
 cout<<pre[z];
   return 0;
}

1:01 backpack problem
(each object can only be taken once, it is required to take the object in a certain space to maximize the value)

There are two ways of writing, one is a two-dimensional array, the other is a one-dimensional array (space saving)

1. Two-dimensional dp template: (It is strange that this method does not need to clear the dp array when there are multiple sample inputs)

State transition equation: dp[i][j]=max(dp[i-1][j],dp[i-1][j-wei[i]]+val[i]);

Basic operation:

if(j>=wei[i])
{
    
    
	dp[i][j]=max(dp[i-1][j],dp[i-1][j-wei[i]]+val[i]);//i为面对第几个物体了
    //j为所占的空间大小下,dp数组存的是对应的价值
}
else//这种写二维的方式可不清空 
{
    
    
	dp[i][j]=dp[i-1][j];
}

2. One-dimensional dp template (remember to clear the dp array in this way, and also pay attention to the reverse order from back to front, because this is the previous state, otherwise the calculation will be repeated)

template:

State transition equation: dp[j]=max(dp[j],dp[j-wei[i]]+val[i]);

Basic operation:

memset(dp,0,sizeof(dp));//一维记得清空 
for(int i=1;i<=n;++i)
{
    
    
	for(int j=v;j>=wei[i];--j)
	{
    
    
		dp[j]=max(dp[j],dp[j-wei[i]]+val[i]);
	}
}
cout<<dp[v]<<endl;

Summary: Simply put, there are two choices for each object, either take it or don't take it. If you don’t take it, it will inherit the previous state, otherwise the dp value of the previous state plus the value of the current object.

Two: Complete knapsack problem
(each object can be taken countless times, it is required to take the object in a certain space to maximize the value)

board:

State transition equation: dp[j]=max(dp[j],dp[jw[i]]+v[i]);

Basic operation:

for(i=1;i<=n;i++)
      for(j=w[i];j<=m;j++)  //注意此处与01背包不同,01为倒序
 
             dp[j]=max(dp[j],dp[j-w[i]]+v[i]);

Optimization: If two items i and j satisfy c[i]<=c[j] and w[i]>=w[j], then item j is removed without consideration.

Guess you like

Origin blog.csdn.net/qq_52172364/article/details/112756610