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

  因为deque最后的数列是单调不降的,因此,我们可以枚举数列中的某个中间数Ai,如果从中间数Ai开始,如果后面的要和这个中间数形成单调不降的序列,那么后面的数必须是单调不降或者单调不升的序列,才能进入deque中,因此为两者长度的和,这就是一个LIS的DP。然后枚举的时候从后往前枚举,复杂度O( n*log n)。这里要注意一点,存在相同元素,因此需要减去两个里面出现Ai次数的最小值!

 //STATUS:C++_AC_281MS_3768KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
//const LL INF=0x3f3f3f3f;
//const int MOD=1000000007,STA=8000010;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int num[N];
int fup[N],fdown[N],dup[N],ddown[N];
int wup[N],wdown[N],cntup[N],cntdown[N];
int upk,downk;
int T,n; int search_up(int *d,int l,int r,int tar)
{
int mid;
while(l<r){
mid=(l+r)>>;
if(d[mid]<=tar)l=mid+;
else r=mid;
}
return l;
} int search_down(int *d,int l,int r,int tar)
{
int mid;
while(l<r){
mid=(l+r)>>;
if(d[mid]>=tar)l=mid+;
else r=mid;
}
return l;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,hig,w;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d",&num[i]);
}
upk=downk=;
dup[]=0x7fffffff,ddown[]=0x80000000;
hig=;
for(i=n;i>=;i--){
w=search_up(dup,,upk,num[i]);
if(w== || num[i]!=dup[w-])cntup[i]=;
else cntup[i]=cntup[wup[w-]]+;
dup[w]=num[i];wup[w]=i;
fup[i]=w;
upk=Max(upk,w+);
w=search_down(ddown,,downk,num[i]);
if(w== || num[i]!=ddown[w-])cntdown[i]=;
else cntdown[i]=cntdown[wdown[w-]]+;
ddown[w]=num[i];wdown[w]=i;
fdown[i]=w;
downk=Max(downk,w+); hig=Max(hig,fup[i]+fdown[i]-Min(cntup[i],cntdown[i]));
} printf("%d\n",hig);
}
return ;
}

HDU-4604 Deque DP的更多相关文章

  1. HDU 4604 Deque 最长子序列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4604 Deque Time Limit: 4000/2000 MS (Java/Others)     ...

  2. hdu 4604 Deque

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

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

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

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

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

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

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

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

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

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

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

  8. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  9. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  10. hdu 3709 数字dp(小思)

    http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...

随机推荐

  1. Akka的fault tolerant

    要想容错,该怎么办? 父actor首先要获知子actor的失败状态,然后确定该怎么办, “怎么办”这回事叫做“supervisorStrategy".   // Restart the st ...

  2. 1046-第K回文数

    描述 回文数是这样一个正整数:它从左往右读和从右往左读是一样的.例如1,111,121,505都是回文数.将1到100,000,000内所有回文数按从小到达排序后,第k个回文数是多少呢? 输入 第一行 ...

  3. Android invalidate()自动清屏,屏幕刷新

    invalidate()是用来刷新View的,必须是在UI线程中进行工作.比如在修改某个view的显示时,调用invalidate()才能看到重新绘制的界面.invalidate()的调用是把之前的旧 ...

  4. 跨域使用jsonp 获取天气预报

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">    ...

  5. C#中this在构造函数时的使用

    今天编程的时候,想要用this来处理构造函数,想了半天没有想起来 后来找了自己以前记录的 http://www.cnblogs.com/chucklu/p/4842766.html public Cu ...

  6. hdu4745Two Rabbits(dp)

    链接 哎..比赛中一下想到了公共子序 之后思维就被局限了 一直在这附近徘徊 想着怎么优化 怎么预处理.. 观看了众多神牛的代码 ..以前觉得自己能写出个记忆化的最长回文长度 还挺高兴的...现在觉得好 ...

  7. [原]Unity3D深入浅出 - 物理材质(Physics Materials)

    在Unity3d中已经配置好了5种常用的物理材质,Bouncy.Ice.Metal.Rubber.Wood,在菜单中依次选择Assets - Import Package - Physics Mate ...

  8. BZOJ_1018_[SHOI2008]_交通堵塞traffic_(线段树)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1018 \(2*n\)的距形,起初没有边相连,之后有三种操作: 1.加边. 2.删边. 3.询问 ...

  9. quartz Cron表达式一分钟教程

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...

  10. amoeba安装与实现amoeba for mysql读写分离

    运行环境 l  CentOS6.3 l  Jdk1.6.0_30 l  amoeba-mysql-binary-2.2.0 l  amoeba:192.168.88.17 l  master1:192 ...