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 n次之后终于AC,贴代码

Posted by lsz at 2011-05-03 00:16:38 on Problem 3903
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	const int MAX = 100000;

	// 输入数据
	long a[MAX];

	// c[i]是以i为结束的最长串长度
	int c[MAX];

	// h[i]是上一个比c[i]大的c[j]位置,j=0~i-1
	vector<int> h(MAX);

	int n;

	while (cin>>n)
	{
		long temp;
		for (int i=0; i<n; i++)
		{
			cin>>temp;
			a[i] = temp;
		}
		c[0] = 1;
		h[0] = -1;

		int count_max_global = 1;
		for (int i=1; i<n; i++)
		{
			// 对a[i],从j=i-1到0搜索,找c[j]>c[i],看情况j--或跳到h[j](c[h[j]]>c[j])做下一次搜索
			c[i] = 0;
			int j = i-1;
			while (j >= 0)
			{
				if (c[j] > c[i])
				{
					if (a[j] < a[i])
					{
						c[i] = c[j];
						j = h[j];
					}else
					{
						j--;
					}
				}else
				{
					j = h[j];
				}
			}

			c[i]++;

			h[i] = -1;
			int k = i-1;
			while (k >= 0)
			{
				if (c[k] > c[i])
				{
					h[i] = k;
					break;
				}else
				{
					k = h[k];
				}
			}

			if (c[i] > count_max_global)
			{
				count_max_global = c[i];
			}
		}
		cout<<count_max_global<<endl;
	}

	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