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

为什么MLE啊?感觉没有超啊!难道递归太深,救命啊!

Posted by yogafrank at 2008-07-26 15:48:54 on Problem 1033
用C++写的却用C提交,compile错了好几次!太绰了。改了C++后还是compile error!后来吧#include < iostream.h >之类的改成下面就不错了。
可是居然MLE了,实在是郁闷。那位达人给看看,谢谢了。

# include < string >
# include < iostream > 
# include < fstream >
//# define in cin

using namespace std;

class MyFile
{
public:
	int size;
	int *p;
};


MyFile file[10000];
int conflict[10001];
bool used[10001];
int n, k, con = 0, mid, ori;

int findlastavailable ()
{
	for ( int i = n; i >= 1; i -- )
		if ( !used[i] )
			return i;
	return -1;
}

int findfirst ()
{
	for ( int i = 1; i <= n; i ++ )
		if ( conflict[i] != 0 )
			return i;

	return -1;
}

bool circle ( int last, int now )
{
	if ( ori == now )
	{
		mid = findlastavailable ();
		cout << last << " " << mid << endl;
		used[mid] = true;
		used[last] = false;

		return true;
	}
	if ( !used[now] )
	{
		cout << last << " " << now << endl;
		used[now] = true;
		used[last] = false;

		return false;
	}

	bool value = circle ( now, conflict[now] );

	conflict[last] = 0;
	conflict[now] = 0;
	cout << last << " " << now << endl;
	used[now] = true;
	used[last] = false;

	return value;
}

void release ()
{
	for ( int i = 0; i < k; i ++ )
		delete file[i].p;
}

int main ()
{
	int i, j;
	bool flag = true;

	ifstream in ( "1033.txt" );

	memset ( conflict, 0, sizeof ( conflict ) );
	memset ( used, false, sizeof ( used ) );

	in >> n >> k;

	for ( i = 0; i < k; i ++ )
	{
		int size;

		in >> size;
		
		file[i].p = new int [size];
		file[i].size = size;

		for ( j = 0; j < size; j ++ )
		{
			in >> file[i].p[j];
			used[file[i].p[j]] = true;
		}
	}

	int value = 1;

	for ( i = 0; i < k; i ++ )
	{
		for ( j = 0; j < file[i].size; j ++ )
		{
			if (  value == file[i].p[j] )
				;
		    else if ( !used[value] )
			{
				cout << file[i].p[j] << " " << value << endl;
				used[value] = true;
				used[file[i].p[j]] = false;
				flag = false;
			}
			else
			{
				conflict[file[i].p[j]] = value;
				flag = false;
			}
			value++;
		}
	}

	if ( flag )
	{
		cout << "No optimization needed" << endl;
		release ();
		return 0;
	}

	int index = findfirst ();

	while ( index != -1 )
	{
		ori = index;
		if ( circle ( index, conflict[index] ) )
		{
			cout << mid << " " << index << endl;
			used[mid] = false;
			used[index] = true;
		}

		index = findfirst ();
	}

	release ();

	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