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 |
诡异呀!难道布尔运算速度太慢?下面两段代码,大家看看是不是一样。 __________________________________________________________________________________ # include <stdio.h> # include <stdlib.h> # define N 64 int stick[N]; int used[N]; int n; int sum; int lg; void init() { sum=0; for (int i=0;i<n;i++) { scanf("%d",&stick[i]);//scanf("%d",&stick[i].len); sum+=stick[i]; } } int cmp(const void * a, const void * b) { return (*(int *)b) - (*(int *)a); } bool dfs(int num, int len, int pos) { for (int i=pos;i<n;i++) { if(used[i] == false && len >= stick[i]) { used[i]=true; if(num == 1 && len == stick[i]) { return true; } if(len == stick[i]) { if(dfs(num-1,lg,0)) return true; else {used[i]=0; return false;} } else { if(dfs(num,len-stick[i],i+1)) return true; else if(lg==len) {used[i]=false; return false; } } used[i]=false; } } return false; } void solve() { qsort(stick,64,sizeof(stick[0]),cmp); for (int i=stick[0];i<=sum;i++) { if (sum%i == 0) { for (int j=0;j<=n;j++) used[i]=false; if (i==sum){ printf("%d\n",sum); break ; } lg=i; if(dfs(sum/i,i,0)) { printf("%d\n",i); break; } } } } int main() { while (scanf("%d",&n),n>0) { init(); solve(); } return 0; } ___________________________________________________________________________________________ #include <stdio.h> #include <stdlib.h> #include <memory.h> #define max 64 int sl[max],vis[max],n; int sum,lg; int cmp(const void * a, const void * b) { return (*(int *)b) - (*(int *)a); } int DFS(int times,int le,int pos) { int i; for(i=pos;i<n;i++) { if(!vis[i]&&le>=sl[i]) { vis[i]=1; if(times==1&&le-sl[i]==0) { return 1;} if(le-sl[i]==0) { if(DFS(times-1,lg,0)==1) return 1; else{vis[i]=0;return 0;} } else { if(DFS(times,le-sl[i],i+1)==1) return 1; else if(lg==le) {vis[i]=0;return 0;} } vis[i]=0; } } return 0; } int main() { int i; while(scanf("%d",&n),n>0) { memset(sl, 0, sizeof(sl)); sum=0; for(i=0;i<n;i++) { scanf("%d",&sl[i]); sum+=sl[i]; } qsort(sl, max, sizeof(int),cmp); for(i=sl[0];i<=sum;i++) { if(sum%i==0) { memset(vis, 0, sizeof(vis)); if(i==sum){ printf("%d\n",sum); break;} lg=i; if(DFS(sum/i,i,0)){ printf("%d\n",i); break;} } } } return 0; } ———————————————————————————————————————————————— 第一个tle,第二个ac Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator