(Hdu step 4.3.5) Sticks (synthesis of n wooden rods into several equi-length wooden rods to obtain the minimum length of the merged wooden rods), hdu4.3.5

Source: Internet
Author: User

(Hdu step 4.3.5) Sticks (synthesis of n wooden rods into several equi-length wooden rods to obtain the minimum length of the merged wooden rods), hdu4.3.5

Question:

Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 364 Accepted Submission (s): 116
 
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 the 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
 
 
SourceACM summer training team exercise session (7)
Recommendlcy


Question Analysis:

DFS.



The Code is as follows:

/** E. cpp ** Created on: February 25, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <cstring> using namespace std; const int maxn = 65; bool visited [maxn]; // used to mark whether an int values [maxn] has been used for a wooden rod; // result sequence bool flag; // used to mark the use of a method that has been successfully found int parts; // The number of parts of the wood rod int len; // The length of the wood rod such as int n; // root number of the sequence of the original wooden rod/*** deep search. * Pos: Index of the current wooden stick * cur: the length of the current wooden stick * cnt: number of existing wooden sticks */void dfs (int pos, int cur, int cnt) {if (cur = len) {// if the current wood rod length = The specified wood rod length cnt ++; // Number of splices + 1if (cnt = parts) {// if the number of splices is equal to or equal to the specified number of sticks, flag = true; // mark the flag as true, indicating that the return has been successful; // return} // if the number of equal-length wood rods dfs (, cnt) has not been matched ); // then continue to fight} if (pos> = n) {// if the index of the currently searched wooden rod has exceeded the range of return; // then return, failed} int I; for (I = pos; I <n; ++ I) {// sweep backward from the current wood stick if (visited [I] = false & valu Es [I] + cur <= len) {// if this wooden rod has not been accessed & length of the current wooden rod + Length of the assembled wooden rod <= lenvisited [I] = true; // mark the wooden stick as having accessed dfs (I + 1, values [I] + cur, cnt ); // search for visited [I] = false on this basis; // rollback/*** pruning. * If a scheme has been found, I still don't want to understand the pruning of * pos. When it is not added, TLE is returned. **/if (flag = true | pos = 0) {return ;}}} int main () {while (scanf ("% d ", & n )! = EOF, n) {int maxLen =-1; int sum = 0; int I; for (I = 0; I <n; ++ I) {scanf ("% d", & values [I]); if (values [I]> maxLen) {maxLen = values [I]; // calculate the maximum length of all wood rods} sum + = values [I]; // calculate the total length of all sticks}/*** enumerate all cases starting from the maximum length of a single stick. * Why does the enumeration start from the maximum value of the root stick instead of the minimum value? Because of the minimum value, * wood sticks with a longer length than the minimum value will inevitably be discarded. this obviously does not match the meaning */for (len = maxLen; len <sum; ++ len) {if (sum % len = 0) {// if the length is, the wooden stick can be classified into memset (visited, false, sizeof (visited); parts = sum/len; // calculates the number of wooden sticks that can be divided into under this length. flag = false; // reset the flag to falsedfs (0, 0, 0); // start searching if (flag = true) {// if break is successful; // skip loop }}printf ("% d \ n", len); // output the minimum length of the merged wood rod} 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.