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

860ms怎样才能优化到200多ms啊?

Posted by colinzhong at 2009-07-16 19:30:14 on Problem 2796
#include <iostream>
using namespace std;

struct point
{
	int left;
	int right;
};

__int64 data[100005];
point flag[100005];			//标记每个点的左右范围
int n;

int main(int argc, char* argv[])
{
	cin >> n;
	data[0] = data[n + 1] = -1;
	int i;
	__int64 sum[100005] = {0}; 
	for(i = 1; i <= n; i++)
	{
		scanf("%I64d",data + i);
		sum[i] = sum[i - 1] + data[i];
	}
	flag[1].left = 1;
	for(i = 1; i <= n; i++)
	{	
		if(data[i] > data[i - 1])
		{
			flag[i].left = i;
		}
		else //data[i] <= data[i - 1]
		{
			int j = i - 1;
			while(flag[j].left >= 1 && data[i] <= data[flag[j].left - 1])
			{
				j = flag[j].left - 1;
			}
			flag[i].left = flag[j].left;
		}
	}
	flag[n].right = n;
	for(i = n; i >= 1; i-- )
	{
		if(data[i] > data[i + 1])
		{
			flag[i].right = i;
		}
		else 
		{
			int j = i + 1;
			while(flag[j].right <= n && data[i] <= data[flag[j].right + 1])
			{
				j = flag[j].right + 1;
			}
			flag[i].right = flag[j].right;
		}
	}
	int k;
	__int64 max = -1;
	for(i = 1; i <= n; i++)
	{
		__int64 temp = sum[flag[i].right] - sum[flag[i].left - 1];
		if(temp * data[i] > max)
		{
			max = temp * data[i];
			k = i;
		}
	}
	printf("%I64d\n%d %d\n", max, flag[k].left, flag[k].right);
	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