light oj 1421

来源:互联网 发布:逆战卡数据教程 编辑:程序博客网 时间:2024/06/07 08:04

1421 - Wavio Sequence
   PDF (English)StatisticsForum
Time Limit: 4 second(s)Memory Limit: 32 MB

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

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

2.      The first (n+1) integers of Wavio sequence make a strictly increasing sequence.

3.      The last (n+1) integers of Wavio sequence make a strictly decreasing sequence.

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

For example 1, 2, 3, 4, 5, 4, 3, 2, 1 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 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

Input starts with an integer T (≤ 12), denoting the number of test cases.

Each case starts with a line containing an integer N (1 ≤ N ≤ 105) denoting the number of elements in the sequence. The next line contains N space separated integers between -108 to 108, that form the sequence.

Output

For each case, print the case number and the length of the maximum possible Wavio sequence.

Sample Input

Output for Sample Input

3

10

1 2 3 4 5 4 3 2 1 10

14

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

5

1 2 3 4 5

Case 1: 9

Case 2: 7

Case 3: 1


要求序列是单峰,且是奇数,那么枚举中心点就可以了额,看清题目对序列的定义



#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <map>#include <bits/stdc++.h>using namespace std;const int N = 1e5+7;int a[N], b[N], c[N];int get(int x,int *h,int l,int r){    while(l<=r)    {        int mid=(l+r)/2;        if(h[mid]<x) l=mid+1;        else r=mid-1;    }    return l;}int main(){    int t;    scanf("%d", &t);    int ncase=1;    while(t--)    {        int n;        scanf("%d", &n);        int l[N], r[N];        for(int i=1;i<=n;i++) scanf("%d", &a[i]);        int k=2;        memset(b,0,sizeof(b));        memset(c,0,sizeof(c));        l[1]=a[1], b[1]=c[1]=1;        for(int i=2;i<=n;i++)        {            if(a[i]>l[k-1])            {                l[k++]=a[i];            }            else            {                int pos=get(a[i],l,1,k-1);                l[pos]=a[i];            }            b[i]=k-1;        }        k=2;        r[1]=a[n];        for(int i=n;i>=1;i--)        {            if(a[i]>r[k-1])            {                r[k++]=a[i];            }            else            {                int pos=get(a[i],r,1,k-1);                r[pos]=a[i];            }            c[n-i+1]=k-1;        }        int ans=0;        for(int i=1,j=1;i<=n;i++)        {            ans=max(min(b[i],c[n-i+1])*2-1,ans);        }        printf("Case %d: %d\n",ncase++,ans);    }    return 0;}




0 0