题目链接: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 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
 
题目大意:给定一个序列A,依次从A中取数,可放入从前端放入B序列,也可从放入B序列尾部,也可直接丢弃,要保证B序列为非递减序列,求B序列的最大长度
 
解题思路:假设只能从一端加入B序列,那么这就是一个求最大非递减子序列的问题。现在因为可以从两端插入B序列,所以要同时求最大非递增子序列与最大非递增子序列即可。具体解法如下:从A序列最后一个数开始依次找以第i个数为开头的最大非递增子序列长度与最大非递减子序列长度,然后求和找总的最大长度。(注意因为求的是非递增和非递减子序列,当序列中出现想等的数的时候要减去重复算的长度)
 #include<cstdio>
#include<cstring>
int a[],z[],j[],zm[],jm[];
// z中存的是以i个数起始的最长递增子序列的最大长度,j中存的递减子序列的长度,zm长度为i的递增序列的初始项,jm递减序列的初始项
int lz,lj;
void findzeng(int index,int x,int l)//找到大于x的子序列,zm单调减
{
int f,t,h=l;
/* if(x>zm[1])
{
z[index]=1;
zm[1]=x;
} */
f=;
while(f<=h)//二分法查找
{
t=(f+h)/;
if(x<=zm[t])f=t+;
else h=t-;//x>zm[t]的情况
}
z[index]=f;
if(f>lz)
{
lz=f;
zm[f]=x;
}
else if(x>zm[f])
zm[f]=x;
}
void findjian(int index,int x,int l)
{
int f,t,h=l;
f=;
while(f<=h)//j[n]单调增
{
t=(f+h)/;
if(x>=jm[t])
f=t+;
else
h=t-;
}
j[index]=f;
if(f>lj)
{
lj=f;
jm[f]=x;
}
else if(jm[f]>x)
jm[f]=x;
}
int main()
{
// freopen("1.in","r",stdin);
int t,n,i,maxl,temp,k;
scanf("%d",&t);
while(t--)
{
maxl=;
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
memset(zm,,sizeof(zm));
memset(jm,,sizeof(jm));
z[n]=;j[n]=;
zm[]=a[n];jm[]=a[n];
lz=;lj=;
for(i=n-;i>;i--)
{
findzeng(i,a[i],lz);
findjian(i,a[i],lj);
}
for(i=;i<=n;i++)
{
if(maxl>=n-i+)
break;
temp=z[i]+j[i]-;
k=;
while(z[i]-k>&&zm[z[i]]==zm[z[i]-k])
{
temp-=;k++;
}
if(temp>maxl)
maxl=temp;
}
printf("%d\n",maxl);
}
return ;
}

HDU 4604 Deque 最长子序列的更多相关文章

  1. hdu 4604 Deque

    http://acm.hdu.edu.cn/showproblem.php?pid=4604 将原数组根据其大小关系处理后 使其大小全在10^5内 处理后为 a1,a2,a3.....an 最优deq ...

  2. hdu 4604 Deque(最长上升与下降子序列-能够重复)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4604 这个题解有点问题,暂时没时间改,还是参考别人的吧 #include <cstdio> ...

  3. HDU 4604 Deque 二分最长上升子序列

    题目大意就是给一个deque 然后有n个数,依次进行操作,每种操作,你可以把这个数放在deque首部,也可以放在尾部,也可以扔掉不管,但是要保证deque中的数是非递减的.最要求deque中最长能是多 ...

  4. HDU 1160 DP最长子序列

    G - FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  5. hdu 4604 Deque(最长不下降子序列)

    从后向前对已搜点做两遍LIS(最长不下降子序列),分别求出已搜点的最长递增.递减子序列长度.这样一直搜到第一个点,就得到了整个序列的最长递增.递减子序列的长度,即最长递减子序列在前,最长递增子序列在后 ...

  6. HDU 4604 Deque(最长上升子序)

    题目链接 本来就对N*log(N)算法不大会....然后各种跪了,求出最长不下降+最长不上升-最少相同元素.求相同元素,用二分求上界搞的.代码里4个二分.... #include <cstdio ...

  7. HDU 4604 deque 最长上升子序列

    枚举每个位置,求以num[i]为起点的最长不下降子序列和以num[i]为结尾的最长不递增子序列. 并且把相同值的个数统计一下,最后要减去算重复了的. 比如: 1 9 4 4 2 2 2 3 3 3 7 ...

  8. HDU 1513 最长子序列

    Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. HDU 4123 (2011 Asia FZU contest)(树形DP + 维护最长子序列)(bfs + 尺取法)

    题意:告诉一张带权图,不存在环,存下每个点能够到的最大的距离,就是一个长度为n的序列,然后求出最大值-最小值不大于Q的最长子序列的长度. 做法1:两步,第一步是根据图计算出这个序列,大姐头用了树形DP ...

随机推荐

  1. Day9 - Python 多线程、进程

    Python之路,Day9, 进程.线程.协程篇   本节内容 操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线 ...

  2. 数据库 ORM框架 ORMLite

    几个ORM框架的比较 先介绍一下ORM的概念,以前也一直听说,不过没详细了解啥意思.其全称叫做对象关系映射(Object Relation Mapping),是一种程序设计技术,用于实现面向对象编程语 ...

  3. ThinkPHP学习 volist标签高级应用之多重嵌套循环、隔行变色(转)

    Action代码: public function index(){ $prod = I("get.prod_en"); $id = I("get.id", 0 ...

  4. 数据逆向传递 unwind segue

    一.简介 unwind segue通过允许你定义一个控制器和其他控制器的关系来扩展segue的概念,这个“关系”先于顺传(流式控制)的方式.基于unwind segue可以实现导航相反的效果,即将界面 ...

  5. solr可用于集群的搜索 【转】

    一. SOLR搭建企业搜索平台 运行环境: 运行容器:Tomcat6.0.20 Solr版本:apache-solr-1.4.0 分词器:mmseg4j-1.6.2  词库:sogou-dic 准备工 ...

  6. iOS9升级后第三方平台无法分享的问题

    最近升级到了Xcode7,在真机调试中发现在初始化微博SDK时程序Crash. 解决办法从微博官网下一个最新的SDK,替换掉工程中的即可. 2.替换微博最新SDK之后成功运行程序,之后发现微信.QQ. ...

  7. 设置windows窗口ICON 【windows 编程】【API】【原创】

    1. ICON介绍 最近开始接触windows 编程,因此将自己所接触的一些零散的知识进行整理并记录.本文主要介绍了如何更改windows对话框窗口的ICON图标.这里首先介绍一下windows IC ...

  8. 跟我学android-Notification

    Notification 可以理解为通知的意思,会出现在通知栏,比如来了一条短信 使用 Notification 有以下3个步骤: 1. 创建 NotificationManager的对象 2.为No ...

  9. 【USACO 1.2.2】方块转换

    [问题描述] 一块N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案.写一个程序来找出将原始图案按照以下列转换方法转换成新图案的最小方式: 1:转90度:图案按顺 ...

  10. JavaScript 框架比较

    显著增强 JavaScript 开发的框架概览 Joe Lennon, 软件开发人员, 自由职业者 简介: 现代 Web 站点和 Web 应用程序倾向于依赖大量客户端 JavaScript 来提供丰富 ...