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

nlogn算法,有点短 我写错了么?用时好多

Posted by ddmbr at 2010-11-12 16:55:24 on Problem 1631
#include<iostream>
using namespace std;
int i, j, n, k, p, a[40000], stack[40000];
int search(int s, int e)
{
	while(s != e)
	{
		if(stack[(s+e)/2] > a[j])
			e = (s+e)/2;
		else
			s = (s+e)/2+1;
	}
	return s;
}
int main()
{
	cin>>k;
	for(i = 1; i <= k; i++)
	{
		cin>>n;
		for(j = 1; j <= n; j++)
			cin>>a[j];
		p = 1;
		stack[1] = a[1];
		for(j = 2; j <= n; j++)
		{
			if(a[j] > stack[p])
			{
				p++;
				stack[p] = a[j];
			}
			else
				stack[search(1, p)] = a[j];
		}
		cout<<p<<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