Rokua P1120 Small Stick [DFS]

Source: Internet
Author: User

Title Description

George had some of the same long little sticks, and he cut the sticks into pieces at random, until the length of each paragraph was no more than 50.

Now, he wants to splice the sticks into the original, but forgets how many sticks and lengths they had at the beginning.

Given the length of each small stick, programming helps him figure out the minimum possible length of the original stick.

input/output format

Input format:

There are two lines in the input file.

The first behavior of a single integer n indicates the total number of small sticks that were chopped off, where n≤60

(Administrator note: To put more than 50 of the length of consciously filtered out, pit a lot of people!) )

The second behavior is n a positive integer separated by an empty number, representing the length of the n small stick.

Output format:

Output file is only one line, indicating the minimum possible length of the required original stick

input/Output sampleInput Sample # #:
95 2 1 5 2 1 5 2 1
Sample # # of output:
6
-----------------------------------------
[Dfs+ Pruning]
Consider enumerating this length and finding the answer interval for [mx,sum] and Len | Sum
Pruning: 1. From the big to the small sort, for a big stick small stick is from the long to the short assembly, embodied in now (without this now optimization also line)
2. When a small stick access, the current processing of the big stick just to reach the required length, there is no need to use more small sticks to replace the newly-accessed small stick, reflected in L+a[i]==len break
3. When a large stick is finished, the first small stick of the next big stick is connected to the l==0 break with the longest remaining stick.

DFS-----> currently uses the number of small sticks, the length of the current assembly, the current from big to small to which sticks
////main.cpp//Small Sticks////Created by ABC on 16/8/13.//copyright©2016 Year ABC. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn= $;intN,a[n],tmp,cnt=0, mx=-1, sum=0;BOOLcmpintAintb) {    returnA>b;}intlen,ok=0, vis[n];voidDfsintTotintLintNow ) {    if(OK)return; if(tot==cnt && l==0) {ok=1;return;} intlast=-1;  for(inti=now;i<=cnt;i++)        if(vis[i]==0&&l+a[i]<=len) {//printf ("T%d%d\n", i,l);            if(L+a[i]==last)Continue; Last=l+A[i]; Vis[i]=1; if(L+a[i]==len) DFS (tot+1,0,1); ElseDFS (tot+1, l+a[i],i+1); Vis[i]=0; if(l==0|| L+a[i]==len) Break; }}intMainintargcConst Char*argv[]) {scanf ("%d",&N);  for(intI=1; i<=n;i++) {scanf ("%d",&tmp); if(tmp<= -) {a[++cnt]=tmp; Mx=max (mx,tmp); sum+=tmp;} } sort (A+1, A +1+cnt,cmp);  for(len=mx;len<=sum;len++)        if(sum%len==0) {OK=0;//cout<<len<< "\ n";Dfs0,0,1); if(OK) Break; } cout<<Len; return 0;}

Rokua P1120 Small Stick [DFS]

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.