POJ 1011 Sticks (deep search + pruning)

Source: Internet
Author: User
Tags sort

Description

George took sticks of the same length and cut them randomly until all parts became at most units long. Now he wants to return sticks to the original state, but he forgot what many sticks he had originally and how long they wer E 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.
Input

The 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.
Output

The output should contains the smallest possible length of original sticks, one per line.
Sample Input

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output

6
5

Main Topic
Given a certain number and length of the sticks, splicing into any new wood of the same length, sticks can not have the remaining, to be able to splice the shortest length of the wood bar.

Thinking of solving problems
1, from small to large enumeration can be stitching the length of the bar, until the conditions to meet the output results. The scope of the enumeration is [the maximum length of a stick, the sum of the length of a stick];
2, local greed: Because the longer the stick on the back of the stick of the greater the binding force, so the small stick to sort, from large to small search, so that you can be as close to the root of the place to prune;
PS: Negative Pure greedy Strategy: example 10 21 14 13 11 9 6 4 3 2 1, if the greedy strategy is fully used, then the stitching order is {21},{14,6,1},{13,4,3,*}, at this time 1 has been last used, so stitching 21 failed. But there is another case that satisfies this length, which is {21},{14,4,3},{13,6,2},{11,9,1}.
3. Pruning thought:
(1) When the use of a length of a stick deep search found and can not be stitched into the target length, the wooden bar, the length of the stick for the current splicing of the wooden bar is not possible, then there is the same length of the stick, directly skip;
(2) in the combination of a Len (len2) value of the first wooden bar, if it is st[i], if the recursive return to find the non-line (that is, recursive back to find the current splicing of the length of the wood bar is still 0), then pruning exit the current bar search, directly back to the previous Len (LEN1) Value the end of the wood bar search, try to replace the other sticks to splice. If you do not return the current search directly, because St[i] cannot be combined with other sticks into the current Len (len2) value, it will certainly be used again in the process of continuing the search St[i] and will not satisfy the condition, so exit directly and prune.

Code Implementation

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
#define MAXN int N,len;
BOOL Flag;
int ST[MAXN],VISIT[MAXN];
    BOOL CMP (int a,int b) {return a>b;} void dfs (int num,int total,int index)//The number of bars to be spelled, the length of the bar currently being stitched, the serial number of the stick used
    if (flag) return;
    if (num==0) {flag=true;  } int lastst=-1;
            Mark the length of the last used stick for (int i=index; i<n; i++) {if (!VISIT[I]&AMP;&AMP;ST[I]!=LASTST)//pruning 1 {
            Lastst=st[i];
                if (Total+st[i]<len) {visit[i]=1;    DFS (NUM,TOTAL+ST[I],I+1);
                Pruning 2 if (flag) return;
                visit[i]=0;   
            if (total==0) return;
                } else if (Total+st[i]==len) {visit[i]=1;
                DFS (num-1,0,0);
                if (flag) return;
                visit[i]=0;
     if (total==0) return;       }}}} int main () {int sum;
        while (~SCANF ("%d", &n)) {sum=0;
        memset (visit,0,sizeof (visit));
        if (n==0) break;
            for (int i=0; i<n; i++) {scanf ("%d", &st[i]);
        Sum+=st[i];
        } sort (st,st+n,cmp);
        int t=0;
            for (len=st[0]; len<=sum; len++) {memset (visit,false,sizeof (visit));
            Flag=false;
                if (sum%len==0) {int num=sum/len;
                DFS (num,0,0);
                    if (flag) {printf ("%d\n", Len);
                Break
}}}} 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.