HDU-1455-Sticks [DFS] [pruning optimization]

Source: Internet
Author: User

Sticks


Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 4755 Accepted Submission (s): 1318

Problem DescriptionGeorge took sticks of the same length and cut them randomly until all parts became at most 50 units long. now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were
Originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.

InputThe input contains blocks of 2 lines. the first line contains the number of sticks parts after cutting, there are at most 64 sticks. the second line contains the lengths of those parts separated by the space. the last line of
File contains zero.

OutputThe output file contains the smallest possible length of original sticks, one per line.

Sample Input

95 2 1 5 2 1 5 2 141 2 3 40
Sample Output
65

Code:

Pruning optimization is nothing more than removing the impossible subtree by correct judgment before writing the code to reduce the running time. Of course, correct and accurate inference is required.

In this question:

Narrow the sum to a [max] To sum/2,

Only the first input data can be determined, saving time.

# Include "stdio. h "# include" iostream "# include" algorithm "# include" string. h "using namespace std; int a [70], sign [70], n, sum; int cmp (int a, int B) {if (a> B) return true; return false;} int dfs (int shao, int p, int I, int num) // The length to be supplemented by the current wooden stick, p is the subscript of the element {// I is the length of each wooden stick tried, num is the nth wooden stick int j; if (num * I = sum) return 1; bool flag; for (j = p + 1; j <= n; j ++) if (! Sign [j] & shao> = a [j]) // short, finding a number can reduce short by {sign [j] = 1; if (shao = a [j]) flag = dfs (I, 0, I, num + 1 ); // if it can be reduced to 0, the next wooden stick is spliced with else flag = dfs (shao-a [j], j, I, num ); // otherwise, search for the next length sign [j] = 0; if (p = 0) return flag; else if (flag) return flag; while (a [j] = a [j + 1]) j ++; // directly skip the same} return 0 ;}int main () {int I; while (scanf ("% d", & n), n) {sum = 0; for (I = 1; I <= n; I ++) // I don't know why I have to store {scanf ("% d", & a [I]); sum + = a [I];} sort (a + 1, a + n + 1, cmp); memset (sign, 0, sizeof (sign); int ans = sum; for (I = a [1]; I <= sum/2; I ++) // from the longest sticks in turn back if (sum % I = 0 & dfs (I, 0, I, 0 )) {ans = I; break;} printf ("% d \ n", ans);} return 0 ;} /* 6440 40 30 35 26 15 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 42 42 41 10 4 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 25 39 40 40 40 40 40 40 40 40 40 18 17 16 15 40 40 40 40 40 40 40915 3 2 11 4 1 8 8 8 */

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.