| ||||||||||
| 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 | |||||||||
从1011改过来的果真超时了,真是奇怪啊。原来1011的数据真的弱了。In Reply To:不剪枝会不超时把?给大家几组数据测试测试? Posted by:ssadwlll at 2008-07-01 14:15:24 大牛帮忙剪剪啊!!!
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 4;
int sticks[20];
bool used[20];
int m, side;
bool cmp ( int a, int b )
{
return a <= b;
}
bool search ( int count, int left, int pos )
{
bool flag = false;
if ( count == N )
return true;
if ( left == side )
flag = true;
for ( int i = 0; i < m; i++ )
{
if ( !used[i] )
{
if ( left > sticks[i] )
{
used[i] = true;
if ( search ( count, left - sticks[i], pos + 1 ) )
return true;
used[i] = false;
if ( flag )
return false;
}
else if ( left == sticks[i] )
{
used[i] = true;
if ( search ( count + 1, side, 0 ) )
return true;
used[i] = false;
return false;
}
}
}
return false;
}
bool isbigger ()
{
for ( int i = 0; i < m; i++ )
if ( sticks[i] > side )
return true;
return false;
}
int main()
{
int t;
freopen ( "1909.txt", "r", stdin );
for ( cin >> t; t > 0; t-- )
{
cin >> m;
for ( int i = 0; i < m; i++ )
cin >> sticks[i];
sort ( sticks, sticks + m, cmp );
int len = 0;
for ( int j = 0; j < m; j++ )
len += sticks[j];
side = len / N;
if ( len % 4 != 0 || isbigger () )
{
cout << "no\n";
continue;
}
memset ( used, false, sizeof ( used ) );
if ( search ( 0, side, 0 ) )
cout << "yes\n";
else
cout << "no\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