Description
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.
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