题目链接: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. hdu 1525 Euclid's Game 博弈论

    思路:两个数a和b,总会出现的一个局面是b,a%b,这是必然的,如果a>=b&&a<2*b,那么只有一种情况,直接到b,a%b.否则有多种情况. 对于a/b==1这种局面, ...

  2. swift苹果的下一代语言

    http://numbbbbb.github.io/the-swift-programming-language-in-chinese/chapter1/01_swift.html 有时间再看,bas ...

  3. 13. 求链表倒数第k个节点

    题目:输入1个链表,输出该链表倒数第k个节点,有头指针和尾指针.链表倒数第0个节点是链表的尾指针节点. 代码: /* 尾指针是倒数第0个,则倒数第k个是正数第len-k个,计算len */ #incl ...

  4. redis的key过期时间

    public void set(String key,String value,int liveTime){ this.set(key, value); this.getJedis().expire( ...

  5. nginux做反向代理配置文件

    做反向代理的配置文件最好单独创建一个文件,然后在主配置文件中使用 include nginx-test.config;  这样的方式来导入. 配置代码如下: ## Basic reverse prox ...

  6. C#连接SQLite的字符串

    一.C#在不同情况下连接SQLite字符串格式 1.Basic(基本的) Data Source=filename;Version=3; 2.Using UTF16(使用UTF16编码) Data S ...

  7. php简陋版实现微信公众号主动推送消息

    推荐一个网站www.itziy.com csdn免积分下载器.pudn免积分下载器.51cto免积分下载器www.verypan.com 百度网盘搜索引擎www.94cto.com 编程相关视频教程. ...

  8. DOM4J介绍与代码示例【转载】

    DOM4J是dom4j.org出品的一个开源XML解析包.Dom4j是一个易用的.开源的库,用于XML,XPath和XSLT.它应用于Java平台,采用了Java集合框架并完全支持DOM,SAX和JA ...

  9. Git教程(10)git比较复杂的功能

    1,只拣选某分支中的一个提交,然后把它合并到当前分支 $ git cherry-pick e43a6fd3e94888d76779ad79fb568ed180e5fcdf 2,Rerere 它是一种重 ...

  10. WPF程序中处理Windows消息

    首先通过WindowInteropHelper类,我们可以获取WPF Window的Handle. WindowInteropHelper helper = new WindowInteropHelp ...