POJ 1742 Coins (Backpack problem)

Source: Internet
Author: User

Question: How many kinds of money can be formed with a certain amount of paper money and a nominal value without exceeding the total amount of money?
Thought: I thought of multiple backpacks. I wrote it with multiple backpacks, tle. Later I used binary optimization or tle. Later I checked the dis and said I would use the priority queue to search for information online, did not understand, later look at acmj1991 blog, said that the use of array marking, and then do not understand Shenma is the array marking method. Later I learned that it is actually a conventional method, but it is very powerful. Specifically, for each item, the amount of circulating money ranges from its own value to the total value. It is determined at each cycle. If this value does not appear, ans adds 1. flag [I] indicates whether the value I exists. num [I] indicates the value of the current item, and sum indicates the quantity of the current item, then if (flag [I-num [I] & cnt [I-num [I] <sum &&! When the flag [I] is created, ans adds 1. Cnt [I] indicates the current number of items used when the value of the current item is I.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <string. h>
Using namespace std;
 
# Define CLR (arr, val) memset (arr, val, sizeof (arr ))
Const int n= 100010;
Int num [110];
Bool flag [N];
Int cnt [N];
Int main (){
// Freopen ("1.txt"," r ", stdin );
Int n, totalvalue;
While (scanf ("% d", & n, & totalvalue) & n & totalvalue ){
For (int I = 0; I <n; ++ I)
Scanf ("% d", & num [I]);
CLR (flag, false );
Flag [0] = true;
Int sum = 0, ans = 0;
For (int I = 0; I <n; ++ I ){
Scanf ("% d", & sum );
CLR (cnt, 0 );
For (int j = num [I]; j <= totalvalue; ++ j ){
If (flag [j-num [I] & cnt [j-num [I] <sum &&! Flag [j]) {
Flag [j] = true;
Ans ++;
Cnt [j] = cnt [j-num [I] + 1;
}
}
}
Printf ("% d \ n", ans );
}
Return 0;
}
Author: wmn_wmn

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.