hdu 4604 Deque(DP递增递减序列,4级)

Deque

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

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
 
Recommend
liuyiding
 
思路:从后往前求非递增,非递减,在记录相同元素的个数,结果就是 max(asc[i]+dsc[i]-same[i]);
求递增递减就不用说,如果没有相同元素的话很容易就知道结果了。那same怎么求呢?为啥这式子就对呢?
举个例子说,就10个元素的序列 2 2 2 2 1 1 2 2 2 3 结果就是10
要从后往前求,就先去个反 3 2 2 2 1 1 2 2 2 2
求非递减的最长序列是 2 2 2 2 2 2 2
非递增的最长序列是 3 2 2 2 2 2 2 2
然后去重就好了,但实际上这样的结果不是最优的,因为 1 1 是可以插入进来的,这个很奇妙的一幕就发生了,我们用二分优化这个求序列DP时
刚好就可以把序列1 1 插进来了变成1 1 2 2 2 2 2 这样去除重复元素就正确了。
题解就TMD的两三句话,智商捉鸡理解了老半天,大神们都不为我们小菜们思考下,解释下same。
 
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 using namespace std;
 6 const int mm=1e5+9;
 7 const int oo=1e9;
 8 int cas,n;
 9 int f[mm],asc[mm],dsc[mm],same[mm];
10 void get(int*f,int*sc,int*sam)
11 {
12   vector<int>q;
13   int a,b;
14   for(int i=0;i<n;++i)
15   {
16     a=lower_bound(q.begin(),q.end(),f[i])-q.begin();
17     b=upper_bound(q.begin(),q.end(),f[i])-q.begin();
18     if(b==q.size())q.push_back(f[i]);
19     else q[b]=f[i];
20     sc[i]=b+1;
21     sam[i]=min(sam[i],b-a+1);
22   }
23 }
24 int main()
25 {
26   while(~scanf("%d",&cas))
27   {
28     while(cas--)
29     {
30       scanf("%d",&n);
31       for(int i=0;i<n;++i)
32         scanf("%d",&f[i]);
33       reverse(f,f+n);///取反求递增递减,和原序求刚好相反,但不影响答案
34       memset(same,0x3f,sizeof(same));
35       //printf("%d\n",same[3]);
36       get(f,asc,same);
37       for(int i=0;i<n;++i)///取负再求递增,就是原先的递减
38         f[i]=-f[i];
39       get(f,dsc,same);
40       int ans=-oo;
41       for(int i=0;i<n;++i)
42         ans=max(ans,asc[i]+dsc[i]-same[i]);
43       printf("%d\n",ans);
44     }
45   }
46   return 0;
47 }
View Code

 

 

转载于:https://www.cnblogs.com/nealgavin/p/3210334.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值