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 |
为什么tle阿 ,就是dp吧?我的程序和注释 大家看看什么问题 郁闷!! #include <iostream> #include <math.h> using namespace std; int N,M,s; unsigned int findmins(int R,int H,int v,int k); void main() { int Rmin,Rmax,Hmin,Hmax,i,j,temps,tempf; unsigned long s; while(cin>>N>>M) { s=-1; if(N<M*M*(M+1)*(M+1)/4) cout<<0<<endl; //当构不成最小的蛋糕时候 就是体积为 1+2^3+3^3 +..M^3; else { Rmin=M; Hmin=M; Rmax=int(sqrt((N-M*M*(M-1)*(M-1)/4)/Hmin)); Hmax=int((N-M*M*(M-1)*(M-1)/4)/(M*M)); if(M==1) {for(i=Rmin;i<=Rmax;i++) if(N%(i*i)==0) s=s>i*i+2*N/i?i*i+2*N/i:s;} //处理只有一层 else { for(i=Rmin;i<=Rmax;i++) for(j=Hmin;j<=Hmax;j++) { tempf=findmins(i,j,N-i*i*j,2); if(tempf!=-1) { temps=i*i+2*i*j+tempf; s=s>temps?temps:s; } } } //动态规划 if(s!=-1) cout<<s<<endl; //s==-1 无解 else cout<<0<<endl; } } } unsigned int findmins(int R,int H,int v,int k) { int i,j; unsigned long mins=-1; if(v<(M-k+1)*(M-k+1)*(M-k+2)*(M-k+2)/4) { return -1; } //不能构成最简单的 返回-1 else { if(k==M) { bool flag=false; int h; for(i=R-1;i>0;i--) { h=v/(i*i); if(v%(i*i)==0&&h<H) { mins=mins>2*i*h?2*i*h:mins; flag=true; } } if(!flag) return -1; //最后一层无解返回-1 } else { int temps,tempf; for(i=R-1;i>M-k;i--) for(j=H-1;j>M-k;j--) { tempf=findmins(i,j,v-i*i*j,k+1); if(tempf!=-1) { temps=2*i*j+tempf; mins=mins>temps?temps:mins; } //如果是-1的话不参与比较 } } } return mins; //返回最小面积 } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator