(Hdu step 4.3.5) Sticks (to synthesize a number of sticks of equal length with n sticks, to find the minimum of the length of the resultant stick)

Source: Internet
Author: User


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 units lon G. Now he wants to return sticks to the original state, but he forgot what many sticks he had originally and how long they were originally. Him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units is integers greater than zero.
Inputthe input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there is at most 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
SOURCEACM Summer Training Team Practice Competition (vii)

Topic Analysis:


The code is as follows:

/* * e.cpp * * Created on:2015 February 25 * author:administrator * * #include <iostream> #include <cstdio> #inc Lude <cstring>using namespace Std;const int maxn = 65;bool visited[maxn];//used to mark whether a stick has been used int values[maxn];// The result sequence bool flag;//is used to mark the length of an int len;//such a long stick with a scheme that has successfully found a solution int parts;//The number of sticks, int n;//The root number of the original stick sequence/** * deep search. * POS: index of the currently searched stick * Cur: the length of the stick currently being spelled * CNT: The number of sticks currently spelled out */void dfs (int pos,int cur,int cnt) {if (cur = len) {//If the length of the current stick = = specified The length of the stick cnt++;//the number of wooden sticks +1if (cnt = = parts) {//If the number of sticks is good = = The number of sticks specified = true;//flag flag as true, indicating that the return was successful;//returns}// If the specified number of equal length sticks DFS (0,0,CNT) has not been spelled,//Then continue to spell}if (POS >= N) {//If the index of the currently searched stick has gone beyond the range of the stick return;//returns, indicating no success}int i;for (i = pos; I < n; ++i) {//swipe backwards from the current stick if (visited[i] = False && Values[i] + cur <= len) {//If the root stick has not been accessed && the length of the current stick + The length of the stick that has been spelled <=lenvisited[i] = true;//will mark the stick as having visited Dfs (I+1,VALUES[I]+CUR,CNT);//On this basis continue to search visited[i] = false;//rollback/ * * pruning. * If a scenario is currently found, return to the "POS" pruning I have not yet figured out, when not added, Tle. * */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];//calculates the maximum length of all sticks}sum + = values[i];//Calculates the total length of all sticks}/** * Enumerates all cases starting from the maximum length of a single stick. * Why enumerate from the top of the root stick instead of starting with the minimum value? Because of the minimum value, a wooden stick that is larger than the minimum length must be discarded. This is obviously not in accordance with test instructions */for (len = maxlen; len < sum; ++len) {if (sum% Len = = 0) {//If this length, the stick can be equal to memset (visited,false,sizeof (visited));p arts = sum/len;//Calculate how many sticks can be divided by that length flag = false;// Re-set flag to Falsedfs (0,0,0);//Start search if (flag = = True) {//If it has succeeded break;//jump out of the Loop}}}printf ("%d\n", Len);//output to find the smallest length of the composite Stick} return 0;}

(Hdu step 4.3.5) Sticks (to synthesize a number of sticks of equal length with n sticks, to find the minimum of the length of the resultant stick)

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.