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

这里过了,在acm.jlu.edu.cn上没过 还能有进一步优化吗?

Posted by bestfresher at 2006-12-15 23:56:08 on Problem 1011
#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;

int units[100];
int units_number;
int units_length;
int in[100];
bool search( int length , int i = 0 , int j = 0 , int action = 0 , int clength = 0 )
{
	if ( i == units_number )
	{
		return true;
	}
	else
	{
		if ( clength == length )
		{
			return search( length , i + 1 , i + 2 , 0 , 0 );
		}
		if ( action == 0 )
		{
			if ( in[i] )
				return search( length , i + 1 , i + 2 , 0 , 0 );
			else
			{
				in[i] = 1;
				if ( search( length , i , i + 1 , 1 , units[i] ) )
					return true;
				in[i] = 0;
				return false;
			}
		}
		else
		{
			while ( j < units_number )
			{
				if ( units[j]*(units_number - j ) + clength < length )
					break;
				if ( units[j] + clength > length )
				{
					++j;
					continue;
				}
				if ( !in[j] )
				{
					in[j] = 1;
					if ( search( length , i , j + 1 , 1 , clength + units[j] ) )
						return true;
					in[j] = 0;
					++j;
					while ( j < units_number && units[j] == units[j-1] )
						++j;
				}
				else
				{
					++j;
				}
			}
			return false;
		}
	}
}
int main()
{
	while ( scanf( " %d" , &units_number ) , units_number != 0 )
	{
		units_length = 0;
		for ( int i = 0 ; i < units_number ; ++i )
		{
			scanf( " %d" , &units[i] );
			units_length += units[i];
		}
		sort( units , units + units_number ,greater<int>() );
	
		
		for ( int i = units_number ; i > 0 ; --i )
		{
			if ( units_length % i == 0 && units_length / i >= units[0] )
			{
				memset( in , 0 , units_number *sizeof(int) ); 
				int k = units_length / i;
				for ( int j = 0 ; j < units_number ; ++j )
				{
					if ( units[j] == k )
						in[j] = 1;
				}
				if ( search( k ) )
				{
					printf( "%d\n" , units_length / i );
					break;
				}
			}
		}
	}
	return 0;
}

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