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