HDU Rice Card

Source: Internet
Author: User

The idea is: first if the m<5, direct output, if m>5, then take out 5 yuan to buy the most expensive things, so the backpack capacity has become m-5, the number of goods for the n-1 0/1 knapsack problem.

The state transition equation for this problem is: Dp[j]=max{dp[j],dp[j-price[i]]+price[i]},dp[j] represents the purchase of the first item I, the budget is J when the maximum cost.

1#include"iostream"2#include"stdio.h"3#include"Cmath"4#include"algorithm"5#include"Queue"6#include"string.h"7 using namespacestd;8 #defineMX 10059 intPRICE[MX];Ten intDP[MX]; One intMain () A { -     inti,j,k,n,m; -      while(cin>>n,n) the     { -          for(i=1; i<=n;i++) -Cin>>Price[i]; -Cin>>m; +Sort (price+1, price+n+1);//The price of items in the order of small to large -         if(m<5) {cout<<m<<endl;Continue;} +Memset (DP,0,sizeof(DP));//Initialize the value of the backpack A         //Dp[j] Represents the biggest cost of buying a pre-I -item with a budget of J at          for(i=1; i<=n-1; i++) -              for(j=m-5; j>=price[i];j--) -              if(Dp[j]<dp[j-price[i]]+price[i]) dp[j]=dp[j-price[i]]+Price[i]; -cout<<m-price[n]-dp[m-5]<<Endl; -     } -     return 0; in}
View Code

HDU Rice Card

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.