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 |
谁帮我看下哪错了In Reply To:请问有人能够将64的那组BT数据控制在1S之内么? Posted by:zentropy at 2007-09-02 16:03:34 所有数据都能通过,C++提交的时候一直出现RE错误,要崩溃了 #include<iostream> #include<stdio.h> #include<algorithm> #include<functional> using namespace std; bool search(int length,int p); bool find(int l); int wood[100],sol[200];//记录木棒信息,已用的木棒信息 bool use[100];//记录使用情况 int sum,key,n;//砍断后的木棒长度和,当前正在用的木棒,木棒总数 int main() { int i,total; while (scanf("%d", &n) == 1, n) { memset(wood,0,sizeof(wood)); memset(sol,0,sizeof(sol)); memset(use,false,sizeof(use)); total=n; sum=0; for(i=0;i<total;i++) { scanf("%d",wood+i); sum+=wood[i]; } sort(wood,wood+total,greater<int>());//剪枝:将木棒长度从大到小排 if(sum%wood[0]==0&&find(wood[0])) { printf("%d\n",wood[0]); } else{ total/=2;//剪枝:因最长的木棒不合要求,故每根木棍至少由两根木棒合成 do{ while(sum/total<=wood[0])total--; if(sum%total==0) { memset(use,false,sizeof(use)); if(find(sum/total)){printf("%d\n",sum/total);break;} } if(sum%2&&total==2){printf("%d\n",sum);break;}//剪枝:作用好像不太明显 }while(--total); } } return 0; } bool find(int l)//验证该长度是否符合要求 { int p,length,number;//当前指向的木棒,还缺的长度,应合成的数量 length=l; number=sum/l; key=1; sol[0]=-1; p=0; while(--number)//剪枝:最后一根木棒不必验证 { if(search(length,p)) { sol[key++]=-1; length=l; p=0; } else{ number+=2; key--; p=sol[key]+1; length=wood[sol[key]]; use[sol[key]]=false; sol[key]=0; if(number>sum/l||key<=1)return false; } } return true; } bool search(int length,int p) { int i=p; while(length) { while(wood[i]>length||use[i]||i==n)//找到比剩余长度小且未被使用过的木棒 { if(i==n) { key--; length+=wood[sol[key]]; use[sol[key]]=false; i=sol[key]; sol[key]=0; while(wood[i]==wood[i+1])i++; //剪枝:因该长度不合要求,故找下一根不同长度的木棒(很重要) if(sol[key-1]==-1){sol[--key]=0;return false;} } i++; } length-=wood[i]; use[i]=true; sol[key++]=i; i++; } return true; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator