Konjac water first hair blog, is a template title tortured afternoon (laughs)
Luo Gu P1060
code show as below:
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; int chengji[100000000], jiage[30], jiazhi[30]; int n, m;//n为钱数,m为个数 int main(void){ cin>>n>>m; for(int i=0; i<m; i++){ cin>>jiage[i]>>jiazhi[i]; jiazhi[i]=jiage[i]*jiazhi[i]; } for(int i=0 ; I <m; I ++ ) { for ( int J = n-; J> = [I] jiage; J, ) { Chengji [J] = max (Chengji [J], Chengji [J-jiage [I]] + jiazhi [I]); } } COUT << Chengji [n-]; return 0 ; }
This is a one-dimensional array optimized for ease of understanding, paste a two-dimensional array (Valley from Los solution to a problem)
#include <iostream> using namespace std; int f[30][100000]; int w[10000]; int v[10000]; int main() { int n,m; int i,j,k; cin>>m>>n; //提前相乘 for(i=1;i<=n;i++) { cin>>w[i]>>v[i]; v[i]*=w[i]; } for(int i=1; I <= n-; I ++ ) { // most critical location backpack 01, in order to prevent repeatedly applied in the same article, the need to search backwards, 01 which is completely different from the Backpacks backpack for ( int C = 0 ; C < = m; C ++ ) { F [I] [C] = F [I- . 1 ] [C]; IF (C> = W [I]) F [I] [C] = max (F [I] [C ], F [I- . 1 ] [CW [I]] + V [I]); } } COUT << F [n-] [m]; return 0 ; }
One-dimensional array and a two-dimensional array are the two ways of the article when the last bit number i (1 <= i <= m) how can the product takes the maximum value, except that a different starting point.
Thinking in the two-dimensional array of sequence number i in the object is the last one, calculating the remaining volume of j (0 <= j <= n) can take when the maximum product to,
Thinking one-dimensional array is assumed that the i-th items into the bag, how to get the remainder can take maximum.
I want to make a start with a recursive method, but can not find expression backpack space restrictions, so do strike.
Established a 2020 flag, put the problem in a recursive method to do it again XD