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

请高手指教为什么超时,谢谢!

Posted by direfire at 2006-10-05 22:57:27 on Problem 1011
#include <iostream> 
#include <sstream> 
#include <string> 
#include <vector> 
#include <set> 
#include <map> 
#include <algorithm> 
#include <cstdio> 
#include <cstdlib> 
#include <cmath>
#include <math.h>
#include <string.h>
#include <stdlib.h>
using namespace std; 

#define REP(i, n) for(int i = 0; i<(n); i++) 
#define abs(a) ((a) >= 0 ? (a) : -(a)) 
#define inf 999999999 
typedef vector<int> VI; 
typedef vector<string> VS; 
typedef long long i64; 
typedef unsigned long long u64;

int n, sum, len;
int d[65];
int l[65];
int vis[65];

int compare( const void* a, const void* b ) {
   int* arg1 = (int*) a;
   int* arg2 = (int*) b;
   return *arg1 - *arg2; 
}

bool judge(int count, int left, int m)
{
//	printf("11count = %d, left = %d, len = %d\n", count, left, len);
	if (count == 1 && left == 0) return true;
	if (left == 0) {
		m = n;
		left = len;
		count--;
	}
	
	for (int i = m-1; i >= 0; i--)
	{
		if (left > l[i]) return false;
		if (!vis[i] && d[i] <= left) {
			vis[i] = 1;
			if (judge(count, left - d[i], i)) return true;	
			vis[i] = 0;
		}	
	} 
//	printf("22count = %d, left = %d, len = %d\n", count, left, len);
	return false;
}
void go()
{
	l[0] = d[0];
	for (int i = 1; i < n; i++)
		l[i] = l[i-1] + d[i]; 
	for (len = d[n-1]; len <= sum; len++)
	{
		if (sum % len == 0) {
			int count = sum/len;
			memset(vis, 0, sizeof(vis));
			if (judge(count, len, n) == true) {
				printf("%d\n", len); break;
			}
		}	
	}	
}
int main(void)
{
	while(cin>>n)
	{
		if (n == 0) break;
		memset(d, 0, sizeof(d));	
		memset(l, 0, sizeof(l));
		sum = 0;
		for (int i = 0; i < n; i++)
		{
			cin>>d[i];
			sum+=d[i];
		}
		qsort(d, n, sizeof(int), compare);
		go();
	} 
	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