Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:何必都那么复杂呢?晒晒我的代码,计数排序

Posted by jiexianzhu at 2010-08-29 17:58:38 on Problem 1011
In Reply To:何必都那么复杂呢?晒晒我的代码,计数排序 Posted by:lirrrr3377 at 2009-08-03 14:40:35
> 用计数排序的思想多好呢!而且省得排序,省得某剪枝中前一个和后一个一样的情况(两根棍子等长,前一个失败后一个直接跳过),省得再用一个数组存某根棍是否被占用(还得从头扫描),何乐而不为?
> 
> 代码:
> #include <stdio.h>
> #include <stdlib.h>
> #define N 55
> int stick[N],t[N],a;
> 
> // 深搜( 还需的长度, 之后的最长长度, 倒数第几根, 剩余棍子总长);
> //返回 1 为深搜失败, 0 为成功.
> int dfs( int len, int k, int rem, int left) {
>     if( rem==0 )   return 0;
>     if( len==0 )   return dfs(a,a,rem-1,left);
>    	if( k>=N )     k=N-1;
>     if( k>len )    k=len;
>     if( t[k]==0 )
>         while( k>0 && t[--k]==0 );
>     if( k==0 )	return 1;
>     if( len==a ) {
>         t[k]--;
>         if( dfs(len-k,k,rem,left)==0 ) return 0;
>         t[k]++;
>         return 1;
>     }
>     if( k<=len ) {
>         for( ; k>0 && left>=len ; left-= t[k]*k,k--)
> 			if( t[k]>0 ) {
> 				t[k]--;
>                 if( dfs(len-k,k,rem,left)==0 ) return 0;
> 				t[k]++;
> 			}
> 		return 1;
>     }/* Core above */
> }
> 
> int main()
> {
>     int i,k,n,sum;
>     while( scanf("%d",&n), n ) {
>         memset( stick, 0, N*sizeof(int) );
>         for( sum=k=0; n--; ) {
>             scanf("%d",&i);
>             stick[i]++;
>             if( i>k ) k=i;
>             sum +=i;
>         }
> 		for( a=k; a<=sum; a++ )
>             if( sum%a==0 ) {
>                 for( i=0; i<N; i++)
>                     t[i]=stick[i];
> 				if( dfs(a,a,sum/a,sum)==0 )
>                     break;
>             }
> 		printf("%d\n",a);
>     }
>     system("PAUSE");
>     return 0;
> }
> 
> 
> 这个代码瞬出 64的那个BT用例:
> 64
> 40 40 30 35 35 26 15 40 40 40 40 40 40 40 40 40 40 40 40 40 40 
> 40 40 43 42 42 41 10 4 40 40 40 40 40 40 40 40 40 40 40 40 40 
> 40 25 39 46 40 10 4 40 40 37 18 17 16 15 40 40 40 40 40 40 40 40
> 
> 
> 最后表达一下对那些直接复制粘贴本段代码之人的强烈鄙视。

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator