POJ 1011 sticks, pruning divine questions

Source: Internet
Author: User

Wooden Stick
Time limit:1000ms Memory limit:10000k
Total submissions:118943 accepted:27429
Description
George brought a set of sticks of equal length. and cut them randomly. So that the length of each stick is not more than 50 units of length. He then tried to restore the sticks to a pre-cut state, but forgot how many sticks and the initial lengths of the sticks were initially. Please design a program to help George calculate the possible minimum length of the stick. The length of each stick is represented by an integer greater than 0.
Input
The input includes multiple sets of data, each of which consists of two rows.

The first line is an integer that does not exceed 64, indicating how many knots of sticks are in common after the chop. The second line is truncated later. The length of the sticks of each section obtained. After the last set of data, it is a zero.


Output
For each set of data, output the smallest possible length of the original stick, one row for each group of data.
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
6

5


#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;bool CMP (int a, int b) {return a>b;} int Sticks[100];bool used[100];int N, Sticklen, len;bool dfs (int i,int l,int t)//i is the number of sticks that are currently being tried, L is the length needed to spell a full stick,        T initial value for all stick total length {if (l==0) {T-=len;        if (t==0) return true;        for (i=0; used[i]; ++i);        Pruning 1: When searching for the next big stick, find the first small stick that has not been used to start used[i]=1;        Because the first root found is definitely the longest, it must be used, so the next root starts the search for if (DFS (I+1,LEN-STICKS[I],T)) return true;        used[i]=0;    T+=len;            } else {for (int j=i; j<n; ++j) {if (j>0&& (Sticks[j]==sticks[j-1]&&!used[j-1]))            Pruning 2: If the front and back two lengths are equal, assume that the previous root is not used.            That is, from the front of the root start to search for the correct results, then from this root will certainly not search the correct results, this pruning power larger continue; if (!used[j]&&l>=sticks[j]) {///Pruning 3: The simplest pruning.                Want to spell a big stick also need length l>= current small stick length, talent choose l-=sticks[j];                Used[j]=1; if (Dfs (j,l,t)) reTurn true;                L+=STICKS[J];                used[j]=0;                if (sticks[j]==l)//Pruning 4: Powerful pruning, the program is executed here to indicate that the search down failed. If the length of the stick just fills the remaining length.            However, after the search fails, you should return to the previous layer of break; }}} return false;}    int main () {int i, Totallen;    BOOL Flag;        while (scanf ("%d", &n), n) {totallen = 0;            for (i=0; i<n; ++i) {scanf ("%d", &sticks[i]);        Totallen + = Sticks[i];        } sort (sticks, sticks+n, CMP);        memset (used, 0, sizeof used);        Flag = false; for (Sticklen = sticks[0]; Sticklen <=totalLen/2;            ++sticklen) {len = Sticklen;                    if (totallen% Sticklen = = 0) if (Dfs (0, Sticklen, Totallen)) {flag= true;                    printf ("%d\n", Sticklen);                Break        }} if (!flag) {printf ("%d\n", Totallen); }} return 0;}


POJ 1011 sticks, pruning divine questions

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.