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,谁给个好算法啊,谢谢啦!!!#include "stdio.h" int besth,ch,bh,d[41],N,M,flag,r; char p[41][2]; void backtrack(int i) {int j; if(i>M) {if(ch==0) {if(bh+2<besth) {besth=bh+2; for(j=1;j<=M;j++) p[j][1]=p[j][0]; } flag=1; } return; } if(r<ch) return; r-=d[i]; if(ch>=d[i]) {ch-=d[i]; p[i][0]='D'; backtrack(i+1); ch+=d[i]; } if(ch+d[i]<=besth) {int mh=bh; if(ch+d[i]>bh) bh=ch+d[i]; ch+=d[i]; p[i][0]='U'; backtrack(i+1); bh=mh; ch-=d[i]; } r+=d[i]; } int main(int argc, char* argv[]) {int i; scanf("%d",&N); while(N--) {scanf("%d",&M); for(i=1;i<=M;i++) scanf("%d",&d[i]); r=0; for(i=1;i<=M;i++) r+=d[i]; besth=1000;ch=0;bh=0;flag=0; backtrack(1); if(flag==1) for(i=1;i<=M;i++) printf("%c",p[i][1]); else printf("IMPOSSIBLE"); printf("\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator