HDU 2546 01 backpack, accessibility hash

Source: Internet
Author: User

Similar to a 01 backpack. HS [N] = 1 indicates whether it is reachable.

It can prove that if the meal card costs more than 5, it will inevitably order the most expensive food.

So the problem is to find M-5 within the order. The closer you get, the better. This is the 01 backpack ..

# Include <iostream> using namespace STD; const int n = 1105; int HS [N]; int price [N]; int main () {int n, m; while (scanf ("% d", & N), n) {memset (HS, 0, sizeof (HS); For (INT I = 0; I <N; I ++) scanf ("% d", & Price [I]); scanf ("% d", & M); If (M <5) {cout <m <Endl; // accidentally write the code as cout <5 <Endl; Wa took half a day to locate the problem continue;} int max =-1; int Index =-1; for (INT I = 0; I <n; I ++) // select the most expensive dish {If (price [I]> MAX) {max = price [I]; Index = I ;}} HS [0] = 1; for (INT I = 0; I <n; I ++) // Cai {if (I = index) continue; // For (Int J = 0; j <= m-5; j ++) // note this error for (Int J = M-5; j> = 0; j --) {If (HS [J] = 1) HS [J + price [I] = 1 ;}int money; For (money = M-5; money> = 0; money --) if (HS [money] = 1) break; cout <m-money-max <Endl;} return 0 ;}

 

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.