HOJ 2085 Wavio Sequence(双lis)

来源:互联网 发布:解除支付宝和淘宝绑定 编辑:程序博客网 时间:2024/05/17 04:40

题目来源:hoj 2085

Wavio Sequence


Time limit : 1 s    Memory limit : 32 mb

Problem Description

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 multiple 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

101 2 3 4 5 4 3 2 1 10191 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 151 2 3 4 5

Sample Output

991
心路历程:对这个数列正向一遍lis,反向一遍lis,然后对所有保存两次lis的点进行遍历,注意最后每个点的wav是两倍的短的lis-1,还有要用nlogn,不然会超时。

代码如下:

#include <iostream>#include <stdio.h>using namespace std;int main(){    int n;    while(scanf("%d",&n)==1)    {        int arr[11111],dp1[11111],dp2[11111],brr[11111],crr[11111],l1,l2,i;//dp1保存正向lis结果,dp2保存反向lis结果,brr辅助dp1,crr辅助dp2,事实上可以不用crr。        for(i=1; i<=n; i++)        {            scanf("%d",&arr[i]);        }        //以下为正向lis(nlogn)        brr[1]=arr[1];        dp1[1]=1;        l1=1;        int index;        for(i=2; i<=n; i++)        {            if(arr[i]>brr[l1])            {                brr[++l1]=arr[i];                dp1[i]=l1;            }            else            {                index=lower_bound(brr+1,brr+1+l1,arr[i])-brr;//注意lb函数的用法                brr[index]=arr[i];                dp1[i]=l1;            }        }        //以下为反向lis        crr[1]=arr[n];        dp2[n]=1;        l2=1;        for(i=n-1; i>0; i--)        {            if(arr[i]>crr[l2])            {                crr[++l2]=arr[i];                dp2[i]=l2;            }            else            {                index=lower_bound(crr+1,crr+1+l2,arr[i])-crr;                crr[index]=arr[i];                dp2[i]=l2;            }        }        //以下求最大值        int max=1;        for(i=1; i<=n; i++)        {            if(max<2*min(dp1[i],dp2[i])-1)//2倍的短的-1            {                max=2*min(dp1[i],dp2[i])-1;            }        }        printf("%d\n",max);    }    return 0;}


期末不挂!

原创粉丝点击