Dfs+ Pruning POJ1011

Source: Internet
Author: User

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 6 using namespacestd;7 8 intN;9 intstick[ -];Ten BOOLmark[ -]; One intsum; A intLen; - intnum; -  the BOOLcmpintAintb) - { -     returnA>b; - } +  - BOOLDfsintSintKintCNT) + { A     if(cnt==num) at         return true; -     Else if(s==len) -         returnDfs0,0, cnt+1); -     Else -     { -         intPre=0; in         inti; -          for(i=k;i<n;i++) to         { +             if(Mark[i]&&stick[i]!=pre&&stick[i]+s<=len)//pruning, if the current total length of the wood bar and the length of the bar is greater than the average length, then delete, no longer downward -             { thePre=Stick[i]; *mark[i]=false; $                 if(Dfs (stick[i]+s,i+1, CNT))Panax Notoginseng                      Break; -mark[i]=true; the                 if(k==0) +                     return false; A             } the         } +         if(i==N) -             return false; $         Else $             return true; -     } - } the  - intMain ()Wuyi { the      while(SCANF ("%d", &n)!=eof&&N) -     { Wusum=0; -          for(intI=0; i<n;i++) About         { $scanf"%d",&stick[i]); -sum+=Stick[i]; -         } -Sort (stick,stick+n,cmp);//sort by from big to small A          for(len=stick[0];len<sum;len++)//The length of the wood bar is the longest wood bar, the length of the total wood bar and +         { the             if(sum%len==0)//The first pruning, the total length of the bar, must be able to divide the length of each piece of wood -             { $memset (Mark,true,sizeof(Mark)); thenum=sum/Len; the                 if(Dfs (0,0,0)) the                      Break; the             } -         } incout<<len<<Endl; the     } the     return 0; About}
View Code

Dfs+ Pruning POJ1011

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.