hdu4604 Deque

Deque

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2051    Accepted Submission(s): 745


Problem Description
Today, the teacher gave Alice extra homework for the girl weren't attentive in his class. It's hard, and Alice is going to turn to you for help.
The teacher gave Alice a sequence of number(named A) and a deque. The sequence exactly contains N integers. A deque is such a queue, that one is able to push or pop the element at its front end or rear end. Alice was asked to take out the elements from the sequence in order(from A_1 to A_N), and decide to push it to the front or rear of the deque, or drop it directly. At any moment, Alice is allowed to pop the elements on the both ends of the deque. The only limit is, that the elements in the deque should be non-decreasing.
Alice's task is to find a way to push as many elements as possible into the deque. You, the greatest programmer, are required to reclaim the little girl from despair.
 

Input
The first line is an integer T(1≤T≤10) indicating the number of test cases.
For each case, the first line is the length of sequence N(1≤N≤100000).
The following line contains N integers A 1,A 2,…,A N.
 

Output
For each test case, output one integer indicating the maximum length of the deque.
 

Sample Input
  
  
3 7 1 2 3 4 5 6 7 5 4 3 2 1 5 5 5 4 1 2 3
 

Sample Output
  
  
7 5 3
 

Source
 

最长上升序列的变形
倒序枚举,对于每一个位置i,
求出包括i的情况下的最长不下降序列和最长不上升序列,
求出最大值即可


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

#define N 100010

const int inf=10000000;

int f1[N],f2[N];
int a[N];
int n,ans;

bool cmp(int x,int y)
{
    return x>y;
}

int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        ans=0;
        scanf("%d",&n);
        for (int i=0; i<n; i++)
            scanf("%d",&a[i]);
        fill(f1,f1+n,inf);
        fill(f2,f2+n,0);
        for (int i=n-1; i>=0; i--)
        {
            *upper_bound( f1, f1+n, a[i] )= a[i];
            *upper_bound( f2, f2+n, a[i], cmp) = a[i];
            int c=  upper_bound(f1,f1+n, a[i]) -lower_bound( f1, f1+n, a[i]);
            ans= max(  upper_bound(f1,f1+n, a[i])-f1+ upper_bound(f2,f2+n, a[i],cmp)-f2 - c  ,ans );
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值