码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 10534. Wavio Sequence

时间:2014-12-06 16:53:45      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

这题是要找一个最长(假设长度为2N-1)的子序列,使得前N个元素递增,后N个元素递减。

由于N比较大,直接上n^2的dp会超时……

用另外的方法……贪心+二分……这应该不算dp了……

好吧……也许可用斜率dp解?额……我不会

最长上升子序列问题:

给出一个由n个数组成的序列x[1..n],找出它的最长单调上升子序列。即求最大的m和a1,
a2……,am,使得a1<a2<……<am且x[a1]<x[a2]<……<x[am]。

动态规划求解思路分析:(O(n^2))

经典的O(n^2)的动态规划算法,设A[i]表示序列中的第i个数,F[i]表示从1到i这一段中以i结尾的最长上升子序列的长度,初始时设F[i] = 0(i = 1, 2, ..., len(A))。则有动态规划方程:F[i] = max{1, F[j] + 1} (j = 1, 2, ..., i - 1, 且A[j] < A[i])。

贪心+二分查找:(O(nlogn))   
开辟一个栈,每次取栈顶元素s和读到的元素a做比较,如果a>s,  则加入栈;如果a<s,则二分查找栈中的比a大的第1个数,并替换。  最后序列长度为栈的长度。  
这也是很好理解的,对x和y,如果x<y且E[y]<E[x],用E[x]替换  E[y],此时的最长序列长度没有改变但序列Q的‘‘潜力‘‘增大。  
举例:原序列为1,5,8,3,6,7  
栈为1,5,8,此时读到3,则用3替换5,得到栈中元素为1,3,8,  再读6,用6替换8,得到1,3,6,再读7,得到最终栈为1,3,6,7  ,最长递增子序列为长度4。


#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int a[10010];
int dp1[10010];
int dp2[10010];
int main()
{
	int i,n,t;
	vector<int>box;
	while(cin>>n)
	{
		for(i=0;i<n;i++)
			cin>>a[i];
		box.clear();
		for(i=0;i<n;i++)
		{
			if(box.empty()||a[i]>box.back())
			{
				box.push_back(a[i]);
				dp1[i]=box.size();
			}
			else
			{
				t=lower_bound(box.begin(),box.end(),a[i])-box.begin();
				box[t]=a[i];
				dp1[i]=t+1;
			}
		}
		box.clear();
		for(i=n-1;i>-1;i--)
		{
			if(box.empty()||a[i]>box.back())
			{
				box.push_back(a[i]);
				dp2[i]=box.size();
			}
			else
			{
				t=lower_bound(box.begin(),box.end(),a[i])-box.begin();
				box[t]=a[i];
				dp2[i]=t+1;
			}
		}
		t=0;
		for(i=0;i<n;i++)
			t=max(t,min(dp1[i],dp2[i]));
		cout<<t*2-1<<endl;
	}
	return 0;
}

 

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

bubuko.com,布布扣

Problem D
Wavio Sequence
Input:
Standard Input

Output: Standard Output

Time Limit: 2 Seconds

 

Wavio is a sequence of integers. It has some interesting properties.

·  Wavio is of odd length i.e. L = 2*n + 1.

·  The first (n+1) integers of Wavio sequence makes a strictly increasing sequence.

·  The last (n+1) integers of Wavio sequence makes a strictly decreasing sequence.

·  No two adjacent integers are same in a Wavio sequence.

For example 1, 2, 3, 4, 5, 4, 3, 2, 0 is an Wavio sequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 is not a valid wavio sequence. In this problem, you will be given a sequence of integers. You have to find out the length of the longest Wavio sequence which is a subsequence of the given sequence. Consider, the given sequence as :

1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1.


Here the longest Wavio sequence is : 1 2 3 4 5 4 3 2 1. So, the output will be 9.

 

Input

The input file contains less than 75 test cases. The description of each test case is given below: Input is terminated by end of file.

 

Each set starts with a postive integer, N(1<=N<=10000). In next few lines there will be N integers.

 

Output

For each set of input print the length of longest wavio sequence in a line.

Sample Input                                   Output for Sample Input

10 
1 2 3 4 5 4 3 2 1 10 
19 
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1 
5 
1 2 3 4 5
           
9 
9 
1

 


Problemsetter: Md. Kamruzzaman, Member of Elite Problemsetters‘ Panel

Source

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Problem Solving Paradigms :: Dynamic Programming :: Longest Increasing Subsequence (LIS)
Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 5. Dynamic Programming
Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 1. Algorithm Design :: Dynamic Programming :: Exercises: Beginner
Root :: Prominent Problemsetters :: Md. Kamruzzaman (KZaman)
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Problem Solving Paradigms :: Dynamic Programming :: Longest Increasing Subsequence (LIS)

Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 3. Problem Solving Paradigms :: Dynamic Programming :: Longest Increasing Subsequence (LIS) - Classical

UVa 10534. Wavio Sequence

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://blog.csdn.net/stl112514/article/details/41776399

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!