程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA 10534-Wavio Sequence(dp_正序逆序最長上升子序列)

UVA 10534-Wavio Sequence(dp_正序逆序最長上升子序列)

編輯:C++入門知識

UVA 10534-Wavio Sequence(dp_正序逆序最長上升子序列)


Wavio Sequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status

Description

Download as PDF

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


Wavio是一個整數序列,具有以下特性:
1、Wavio序列的長度是奇數, 即 L = 2 * n + 1;
2、Wavio序列前 n+1 個整數是遞增序列
3、Wavio序列後 n+1 個整數是遞減序列
如示例 1 2 3 4 5 4 3 2 1 10
最長的 Wavio序列 為 1 2 3 4 5 4 3 2 1 ,所以答案為9
對於輸入序列中的一個整數 ai ,我們設以 ai 為尾的前綴的最長遞增序列的長度為Fi ,如在示例1中對已第3個整數3,從頭開始,以3為尾的遞增序列為1 2 3 ,所以F3=3;
以ai為首的後綴的遞減序列的長度為Gi, 如示例1中第3個整數,以3為開始,遞減序列為3 2 1,所以G3=3 (可以看做求從最後一個元素出發,到3這個位置的最大遞增序列,為1 2 3,所以G3=3)
在我們找遞減序列的時候,可以看做從最後一個元素出發,到當前位置的最大遞增序列,這樣我們對於每一個元素ai 我們可以先求出 a0到ai最大遞增序列 和 an-1到ai的最大遞增序列 ,這樣我們可以得到ai的Wavio序列的值為
2*min( Fi , Gi) - 1 ,最後的結果是這些Wavio序列中的一個最大值。


#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
const int inf=0x3f3f3f3f;
int binsearch(int *a,int r,int key)
{
    int l=0;
    int mid=(l+r)/2;
    while(l<=r){
        if(a[mid]key)
            r=mid-1;
        else
            return mid;
        mid=(l+r)/2;
    }
    return l;
}

int main()
{
    int n,i;
    while(~scanf("%d",&n)){
      int a[10010],b[10010],c[10010];//b保存的是以a[i]為結尾的最長遞增子序列,c保存長度為i的遞增子序列的最末的元素,c是單調遞增的
      int a1[10010],b1[10010],c1[10010];//同上
      for(i=0;i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved