HDU 4604 Deque 最长子序列

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4604

Deque

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


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
 
题目大意:给定一个序列A,依次从A中取数,可放入从前端放入B序列,也可从放入B序列尾部,也可直接丢弃,要保证B序列为非递减序列,求B序列的最大长度
 
解题思路:假设只能从一端加入B序列,那么这就是一个求最大非递减子序列的问题。现在因为可以从两端插入B序列,所以要同时求最大非递增子序列与最大非递增子序列即可。具体解法如下:从A序列最后一个数开始依次找以第i个数为开头的最大非递增子序列长度与最大非递减子序列长度,然后求和找总的最大长度。(注意因为求的是非递增和非递减子序列,当序列中出现想等的数的时候要减去重复算的长度)
 1 #include<cstdio>

 2 #include<cstring>

 3 int a[100010],z[100010],j[100010],zm[100010],jm[100010];

 4 // z中存的是以i个数起始的最长递增子序列的最大长度,j中存的递减子序列的长度,zm长度为i的递增序列的初始项,jm递减序列的初始项

 5 int lz,lj;

 6 void findzeng(int index,int x,int l)//找到大于x的子序列,zm单调减

 7 {

 8     int f,t,h=l;

 9     /* if(x>zm[1])

10     {

11         z[index]=1;

12         zm[1]=x;

13     } */

14         f=1;

15         while(f<=h)//二分法查找

16         {

17             t=(f+h)/2;

18             if(x<=zm[t])f=t+1;

19             else h=t-1;//x>zm[t]的情况

20         }

21         z[index]=f;

22         if(f>lz)

23         {

24             lz=f;

25             zm[f]=x;

26         }

27         else if(x>zm[f])

28             zm[f]=x;

29 }

30 void findjian(int index,int x,int l)

31 {

32     int f,t,h=l;

33     f=1;

34     while(f<=h)//j[n]单调增

35     {

36         t=(f+h)/2;

37         if(x>=jm[t])

38             f=t+1;

39         else

40             h=t-1;

41     }

42     j[index]=f;

43     if(f>lj)

44     {

45         lj=f;

46         jm[f]=x;

47     }

48     else if(jm[f]>x)

49         jm[f]=x;

50 }

51 int main()

52 {

53     // freopen("1.in","r",stdin);

54     int t,n,i,maxl,temp,k;

55     scanf("%d",&t);

56     while(t--)

57     {    

58         maxl=0;

59         scanf("%d",&n);

60         for(i=1;i<=n;i++)

61             scanf("%d",&a[i]);

62         memset(zm,0,sizeof(zm));

63         memset(jm,0,sizeof(jm));

64         z[n]=1;j[n]=1;

65         zm[1]=a[n];jm[1]=a[n];

66         lz=1;lj=1;

67         for(i=n-1;i>0;i--)

68         {

69             findzeng(i,a[i],lz);

70             findjian(i,a[i],lj);

71         }

72         for(i=1;i<=n;i++)

73         {

74             if(maxl>=n-i+1)

75                 break;

76             temp=z[i]+j[i]-1;

77             k=1;

78             while(z[i]-k>0&&zm[z[i]]==zm[z[i]-k])

79             {

80                 temp-=1;k++;

81             }

82             if(temp>maxl)

83                 maxl=temp;

84         }

85         printf("%d\n",maxl);

86     }

87     return 0;

88 }
View Code

 

你可能感兴趣的:(deque)