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 |
这里过了,在acm.jlu.edu.cn上没过 还能有进一步优化吗?#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator