| ||||||||||
| 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 | |||||||||
Re:对没AC的同学一点提示In Reply To:对没AC的同学一点提示 Posted by:PlayerZ at 2008-08-07 08:58:43 #include <iostream.h>
#include <memory.h>
#include <stdlib.h>
int T, S;
int L;
int anLength[65];
int anUsed[65];
int i,j,k;
int Dfs(int nUnusedSticks, int nLeft);
int MyCompare( const void * e1, const void * e2)
{
return *((int *)e2) - *((int *)e1);
}
int main()
{
while(1)
{
cin >> S;
if( S == 0 )
break;
int nTotalLen = 0;
for( int i = 0; i < S; i ++ )
{
cin >> anLength[i];
nTotalLen += anLength[i];
}
qsort(anLength,S,sizeof(int),MyCompare);
for( L = anLength[0]; L <= nTotalLen / 2; L ++ )
{
if( nTotalLen % L)
continue;
memset( anUsed, 0,sizeof(anUsed));
if( Dfs( S,L))
{
cout << L << endl;
break;
}
}
if( L > nTotalLen / 2 )
cout << nTotalLen << endl;
} // while
return 0;
}
int Dfs( int nUnusedSticks, int nLeft)
// nLeft表示当前正在拼的棍子和 L 比还缺的长度
{
if( nUnusedSticks == 0 && nLeft == 0 )
return true;
if( nLeft == 0 ) //一根刚刚拼完
nLeft = L; //开始拼新的一根
for( int i = 0;i < S;i ++)
{
if( !anUsed[i] && anLength[i] <= nLeft)
{
if( i > 0 )
{
if( anUsed[i-1] == false
&& anLength[i] == anLength[i-1])
continue; //剪枝3
}
anUsed[i] = 1;
if ( Dfs( nUnusedSticks - 1,
nLeft - anLength[i]))
return true;
else {
anUsed[i] = 0;//说明不能用i作为
//第1条,
//那么要拆以前的
//棍子,i还可能用
//在以前的棍子里,
//因此要 anUsed[i] = 0;
if( anLength[i] == nLeft || nLeft == L)
return false;//剪枝2、1
}
}
}
return false;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator