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

按升序排超时,按降序就ac(附代码)

Posted by Hirl at 2010-04-08 21:44:37 on Problem 1011
一下午都花在这个上面,结果按照降序排列立马AC
#include<iostream>
#include<algorithm>
using namespace std;

int num;
int sticks[64];
bool state[64];
int lenth;
int cmp(int a,int b)
{
	return a>b;
}
int minStick(int);
bool judge(int,int,int,int);

int main()
{
	while(cin>>num && num)
	{
		int sum = 0;
		for(int i=0;i<num;i++)
		{
			cin>>sticks[i];
			sum += sticks[i];
		}
		cout<<minStick(sum)<<endl;
	}
	return 0;
}

int minStick(int sum)
{
	sort(sticks,sticks+num,cmp);
	for(int i=num;i>=1;i--)
	{
		if(sum % i)
		{
			continue;
		}
		if(sum/i < sticks[0])
		{
			continue;
		}
		memset(state,0,sizeof(state));
		state[0] = true;
		if(judge(sticks[0],sum/i,sum,1))
		{
			return sum / i;
		}
	}
	return -1;
}

bool judge(int len,int goal,int rest,int index)
{
	int wrongstick = 0;
	if(rest == 0)
	{
		return true;
	}
	if(len == goal)
	{
		return judge(0,goal,rest-goal,0);
	}
	for(int i = index;i<num;i++)
	{
		if(state[i])
		{
			continue;
		}
		if(len+sticks[i] > goal)
		{
			continue;
		}
		if(sticks[i] == wrongstick)
		{
			continue;
		}
		state[i] = true;
		if(judge(len+sticks[i],goal,rest,i+1))
		{
			return true;
		}
		wrongstick = sticks[i];
		state[i] = false;
		if(index == 0)
		{
			break;
		}
	}
	return false;
}

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