Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
能过已知的所有数据,BT的64也能过,不过出来很慢,WA中,求助~~~代码如下: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> using namespace std; int snum,t[65],sum,maxx; bool finded,b[65]; //finded表示找到解,b[65]表示stick的使用情况 bool dfs(int x) //传入参数:枚举的长度 { if(x==0) //长度被满足 return true; int i; bool f; f=false; //表示长度能被满足 for(i=snum-1;i>=0;i--) { if(b[i]) continue; else { b[i]=true; if(x-t[i]>=0&&dfs(x-t[i])) f=true; } if(f) break; else b[i]=false; } if(f) return true; else return false; } int main() { int i,j,k,cnt; while(1==scanf("%d",&snum),snum) { memset(b,0,sizeof(b)); memset(t,0,sizeof(t)); sum=0; //stick的长度总和 maxx=0; //stick的最大长度 for(i=0;i<snum;i++) { scanf("%d",&t[i]); sum+=t[i]; if(t[i]>maxx) maxx=t[i]; if(t[i]<minx) minx=t[i]; } sort(t,t+snum); //对输入的stick排序 for(i=maxx;i<=sum;i++) { finded=false; memset(b,0,sizeof(b)); //每次枚举长度都初始化stick的状态 if(sum%i==0) //如果枚举的长度为总和的约数就进入 { finded=true; cnt=sum/i; //求商 for(j=0;j<cnt;j++) { if(dfs(i)) continue; else { finded=false; break; } } } if(finded) { printf("%d\n",i); break; } } } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator