Sticks (poj1011/uva307)

Source: Internet
Author: User

Question:

George has some broken wood sticks, which are obtained by breaking some original wood sticks of equal length, giving the total number of broken wood sticks and their respective lengths, and finding the minimum possible length of the original wood sticks; (that is, grouping some positive integers. Each group is equal to the sum to make the sum as small as possible)

 

At first do poj 32 Ms too, but uva3000 MS are timeout... In addition, a set of Bt data is provided in poj discuss, and the code of the WordPress 0.2sac also runs 3. About 4 seconds. The scalpers in discuss are said to have been playing odd-sex Pruning for 0.01ms. Unfortunately, they haven't found a specific method after searching for a long time. This is a good question .. (It is difficult to access the AC only by referring to various spatial blogs. However, this question is too classic, and search learning pruning is required)

 

For this question, there are generally the following six pruning strategies:

① Sort the length of a wooden rod from large to small to facilitate subsequent selection and operations, which is the premise of some pruning algorithms.

② When enumerating the length of a log rod, the enumerated range is between Max and sum/2. If no proper length is found in this range, the final length of the log rod can only be sum.

③ The length of the enumerated log stick can only be an approximate sum.

④ In the deep search process, if the length of the current wooden rod is the same as that of the previous one, but the previous one is not selected, then the wooden rod will not be selected.

⑤ In the deep search process, if the current is the first piece of a new wooden rod, but if the longest available wooden rod cannot be used successfully, so you don't have to try the wooden sticks behind it. It must have been a problem in the previous fight process.

⑥ In the deep search process, if the currently available wooden rod can be used to add the last part of a wooden rod, but after it is used, it cannot be used to complete subsequent tasks with the remaining wooden rod, so you don't have to try the wooden sticks. It must have been a problem in the previous fight process.

Reference blog: http://www.cnblogs.com/staginner/archive/2011/09/08/2171329.html

My AC code: (for pruning, see comments)

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 # Include <cstdio> # include <algorithm> # include <cstring> using namespace STD; int N, max_len, min_len, sum, ANS, num, flag; int A [10000], V [10000]; bool CMP (const Int & X, const Int & Y) {return! (X <Y);} void search (INT step, int from, int cur) // step indicates the number of original rods to be merged, order indicates how many broken sticks are currently used to gather the step root {// printf ("% d \ n", step, cur); If (cur = ans) {If (step = num) {flag = 1; return;} int K; For (k = 1; k <= N & V [k]; k ++); V [k] = 1; search (Step + 1, 1, a [k]); V [k] = 0; return ;} for (INT I = from; I <= N; I ++) {If (V [I]) continue; If (cur + A [I]> ans) continue; if (A [I] = A [I-1] &! V [I-1]) continue; // pruning 3: The last stick is the same as the current one and never used, then, the same result as the previous result cannot be obtained. // printf ("KK % d \ n", cur, cur + A [I]); int T = A [I] + cur; If (T! = Ans & T + min_len> ans) continue; V [I] = 1; search (step, I + 1, t); V [I] = 0; if (t = ans) return; If (flag | cur = 0) // pruning 4: If one broken rod is used for clustering, if the first broken rod cannot be parsed, return;} int main () {While (scanf ("% d", & n) = 1) is exceeded) {If (n = 0) break; V [0] = 1; A [0] = 210000000; max_len =-1; min_len = 21000000; sum = 0; flag = 0; For (INT I = 1; I <= N; I ++) {scanf ("% d", & A [I]); if (A [I]> max_len) max_len = A [I]; if (a [I] <min_len) min_len = A [I]; Su M + = A [I];} Sort (a + 1, A + n + 1, CMP); For (ANS = max_len; ans <= sum/2; ans ++) // pruning 1: the lower bound of the answer is the longest, and the upper bound is the total length {If (sum % ans! = 0) continue; // pruning 2: the length of the original rod is the approximate number of the total length for (int K = 1; k <= N; k ++) V [k] = 0; num = sum/ANS; search (1, 1, 0); If (FLAG) {printf ("% d \ n", ANS ); break;} If (! Flag) printf ("% d \ n", sum);} 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.