CRB and his Birthday
Title Abstract: knapsack problem, here the value of x items is a * x + B (x > 0) or 0 (x = 0).
Analysis: Sort items by the number of purchases 1. 1 pieces, more than 2 (>1). For one piece the case is 01 backpack. Multiple backpacks are the case for multiple pieces.
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 intdp[2005];6 7 intMain () {8 intT, N, M, A, B, C;9scanf"%d", &T);Ten while(t--) { Onescanf"%d%d", &m, &n); AMemset (DP,0,sizeof(DP)); - for(inti =0; I < n; i++) { -scanf"%d%d%d", &a, &b, &c); the for(intj = m; J >= A; j--)//01 Backpack put items on quantity as a class here is to buy an item -DP[J] = max (Dp[j], dp[j-a] + B +c); - for(intj = A; J <= M; J + +)//the deal here is to buy multiple items (> 1). -DP[J] = max (Dp[j], dp[j-a] +b); + //Note When buying items here for one is, +b, this situation is obviously better than buying A + A + B, to be small. +b does not meet the test instructions, here also can not get +b situation. - } +printf"%d\n", Dp[m]); A } at return 0; -}
CRB and his Birthday 01 backpack + Multiple Backpack