现在的位置: 首页 > 综合 > 正文

HDU 4604 Deque

2017年11月16日 ⁄ 综合 ⁄ 共 2322字 ⁄ 字号 评论关闭

Deque

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
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 A1,A2,…,AN.
 

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
2013 Multi-University Training Contest 1

标准题解是:

考虑题目的一个简化版本:使双端队列单调上升。对于序列 A 和队列 Q,找到队列中最早出现的数字Ax,则Ax将 Q 分成的两个部分分别是原序列中以Ax开始的最长上升和最长下降序列,答案即为这两者之和的最大值。而对于本题,由于存在相同元素,所以只要找到以Ax为起点的最长不下降序列和最长不上升序列的和,然后减去两个里面出现Ax 次数的最小值即可。

代码就是按标准题解写的。

5

5 5 5 5 5     这种数据需要特殊处理,我是加了一个flag数组来记录当前数据为止,有多少个连续相同的数据。

#include<cstdio>
#include<cstring>

const int maxn=110000;

int a[maxn],b1[maxn],b2[maxn],flag[maxn];

int find1(int tmp,int sta,int la)				//处理递增序列
{
    int m=(sta+la)>>1;
    while(!(tmp<b1[m] && tmp>=b1[m-1]) && sta<=la)
    {
        if(tmp<b1[m])
            la=m-1;
        else
            sta=m+1;
        m=(sta+la)>>1;
    }
    return m;
}

int find2(int tmp,int sta,int la)				//处理递减序列
{
    int m=(sta+la)>>1;
    while(!(tmp>b2[m] && tmp<=b2[m-1]) && sta<=la)
    {
        if(b2[m]<tmp)
            la=m-1;
        else
            sta=m+1;
        m=(sta+la)>>1;
    }
    return m;
}

int main()
{
    int T,N;
    int i;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d",&N);
		
        for(i=1;i<=N;i++)
        {
            scanf("%d",&a[N-i+1]);
        }

        int count1=1,count2=1,tmp1,tmp2,x=1,y=1;
        b1[1]=a[1];        b1[0]=-1000000000;
        b2[1]=a[1];        b2[0]=1000000000;
        int max=x+y;
		flag[1]=1;
        for(i=2;i<=N;i++)
        {
	    if(a[i]==a[i-1])
		flag[i]=flag[i-1]+1;
	    else
		flag[i]=1;
            if(a[i]>=b1[count1])
            {
                b1[++count1]=a[i];
                x=count1;
            }
            else
            {
                tmp1=find1(a[i],1,count1);
                b1[tmp1]=a[i];
                x=tmp1;
            }
            if(a[i]<=b2[count2])
            {
                b2[++count2]=a[i];
                y=count2;
            }
            else
            {
                tmp2=find2(a[i],1,count2);
                b2[tmp2]=a[i];
                y=tmp2;
            }
            if(x+y-flag[i]+1>max)
                max=x+y-flag[i]+1;
        }
        printf("%d\n",max-1);
    }
    return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.