UVa 10534 - Wavio Sequence

LIS 朴素算法时间是 n  ^ n, 会超时,所以需要用到时间复杂度nlogn的方式。是用到的二分查找的方式。用一个栈来维护整个序列,当A[i] 大于栈顶的元素,则将A[i]放入栈顶。否则找到恰好不小于A[i]的元素的位置,用A{i]替换他,最后top就是整个序列的最长上升子序列。


/*************************************************************************
    > File Name: UVa10534.cpp
    > Author: Toy
    > Mail: ycsgldy@163.com 
    > Created Time: 2013年05月09日 星期四 07时24分04秒
 ************************************************************************/

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>

using namespace std;
int Case, opt[10005], A[10005], vis[2][10005], top;

void LIS ( int i, int v, int j ) {
    if ( v > opt[top] ) opt[++top] = v;
	else {
		int low = 1, high = top;
		while ( low <= high ) {
			int mid = ( low + high ) / 2;
			if ( v > opt[mid] ) low = mid + 1;
			else high = mid - 1;
		}
		opt[low] = v;
	}
	vis[j][i] = top;
}

int main ( ) {
	while ( scanf ( "%d", &Case ) == 1 ) {
		for ( int i = 1; i <= Case; ++i ) 
			scanf ( "%d", &A[i] );

		top = 0;
		opt[0] = -1;
		for ( int i = 1; i <= Case; ++i ) 
		     LIS ( i, A[i], 0 );

		top = 0;
		opt[0] = -1;
		for ( int i = Case; i >= 1; --i ) 
			LIS ( i, A[i], 1 );

		int ans = -1;
		for ( int i = 1; i <= Case; ++i ) 
			if ( ans < min ( vis[0][i], vis[1][i] ) ) ans = min ( vis[0][i], vis[1][i] );
		printf ( "%d\n", 2 * ans - 1 );
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值