Frozen Throne
Time Limit: 2000/1000ms (Java/Others)
Problem Description:
The Lich King of the undead was paid a salary, and the death knight got an N dollar bill (remember, there is only one bill), in order to prevent himself from dying frequently in battle, he decided to buy himself some props, so He came to the goblin shop.
Death Knight: "I want to buy props!"
Goblin Merchant: "We have three items here, a blood bottle for 150 yuan, a magic potion for 200 yuan, and an invincibility potion for 350 yuan."
Death Knight: "Okay, give me a blood vial."
After he finished speaking, he took out the N dollar bill and handed it to the goblin merchant.
Goblin Merchant: "I forgot to remind you, we don't have the habit of asking for money from guests here. We take any extra money as a tip, hehe."
Death knight:"......"
The death knight thought, instead of giving him money as a tip, he might as well buy some more items himself. Anyway, he will buy it in the future. It's better to buy it early and keep it at home, but let him earn as little as possible.
Now the death knight wants you to help him figure out how much he should tip the goblin merchant at least.
Output:
For each set of test data, please output the minimum amount of money the death knight wastes to the goblin merchant as a tip.
Sample Output:
0
50
problem-solving ideas: complete knapsack problem. There are three kinds of props in this question. You are required to give an N dollar bill, buy as many props as possible, and try to make the goblin merchants as little as possible to earn tips. From this, you can think of the complete backpack problem. The so-called complete backpack usually has n kinds of items (corresponding to the 'props' of this question ) and a backpack with a capacity of V (corresponding to the 'N-yuan banknotes' of this question ), and each item has unlimited pieces available. The volume of the ith item (prop) is w, and the value (face value) is v. Find out which props to buy into the backpack so that the total volume W of these props does not exceed the backpack capacity (banknotes) , and the total value V is the largest. This way goblin merchants earn less tips. The face value of 150, 200, and 350 here is the corresponding w. In this question, the value of the item can be regarded as the item volume w[i]. At this time, when the volume is the largest, it is also the time when the value is the largest. Because the maximum volume will not exceed n, so dp [n]<=n.
State transition equation: dp[vi]=max(dp[vi],dp[vi-v[i]]+w[i]);
AC code:
1 #include<bits/stdc++.h>
2 using namespace std;
3 int dp[10005];
4 const int v[3]={150,200,350},w[3]={150,200,350};
5 int main()
6 {
7 int t,n;
8 cin>>t;
9 while(t--){
10 memset(dp,0,sizeof (dp));
11 cin>> n;
12 for ( int i= 0 ;i< 3 ;++i){ // Number
13 for ( int j=v[i];j<=n;+ +j) // value from small to large enumerate
14 dp[j]=max(dp[j],dp[jv[i]]+ w[i]);
15 }
16 cout<<(n-dp[n ])<< endl;
17 }
18 return 0 ;
19 }