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

彻底崩溃。。。。。TLE, 二分加递归到底怎么写才能过啊?求救!

Posted by howie at 2006-08-13 02:17:25 on Problem 2452
      #include <iostream>
using namespace std;

int a[50001];
int max, n;

int solve(int beg, int end)
{
	if (beg >= end) return 0;
	if (end-beg == 1)
	{
		if (a[end] > a[beg]) 
		{
			if (max < 1)
				max = 1;
		}
		return 0;
	}
	solve(beg, (beg+end)/2);//左边区间
	solve((beg+end)/2, end);//右边区间


	//以下是包括mid点的区间
	int mid = (beg+end)/2;		
	int p = mid-1;
	int q = mid+1;
	if (p == 0 || q == n+1) return 0;	
	int i, j, pp, qq, tmp1, tmp2, tmp3;
	bool flag1 = false, flag2 = false;
	tmp1 = a[mid];
	tmp2 = a[mid];
	tmp3;
	for (i=p; i>=beg; i--)
	{
		if (a[i] > tmp2)
			tmp2 = a[i];
		if (a[i] < tmp1)
		{
			tmp1 = a[i];
			tmp3 = tmp2;
			for (j=q; j<=end; j++)
			{
				if (a[j] > tmp2)
				{
					tmp3 = a[j];
					if (max < j-i)
						max = j-i;
				}
			}
		}
	}
	tmp2 = a[mid];
	for (i=q; i<=end; i++)
	{
		if (a[i] < a[mid]) break;
		if (a[i] > tmp2)
		{
			tmp2 = a[i];
			if (max < i-mid) max = i-mid;
		}
	}
	tmp1 = a[mid];
	for (i=p; i>=beg; i--)
	{
		if (a[i] > a[mid]) break;
		if (a[i] < tmp1)
		{
			tmp1 = a[i];
			if (max < mid-i) max = mid-i;
		}
	}

	return 0;
}

int main()
{
	int i;
	while (scanf("%d", &n) != EOF)
	{
		for (i=1; i<=n; i++)
			scanf("%d", &a[i]);
		
		max = -1;
		solve(1, n);
		printf("%d\n", max);

	}
	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