Wavio Sequence
Time Limit:3000MS
Memory Limit:0KB
64bit IO Format:%lld & %llu Submit Status
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.
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