1. /*
  2. 首先预处理好f g数组
  3. fi :以a[i]为结尾的 最长上升子序列的长度
  4. gi :以a[i]为开始的 最长上升子序列的长度
  5. mxx : 最长上升子序列的长度
  6. 线段树优化 nlogn
  7. (不包含a[i]==0)
  8.  
  9. 显然把所有0换成x 只可能是mxx变成mxx+1
  10.  
  11. 然后我们考虑一对 i j (下标)
  12. 若 f[i]+g[j]==mxx 则 所有a[i]+1~~~a[j]-1之间的x
  13. 他们对用的lis长度为mxx+1
  14. 然后枚举i j凉了
  15. 对于一个i 我们只需要找到 他后面的 一个j 满足 f[i]+g[j]==mxx 并且a[j]最大
  16. 然后维护bg[i] 表示长度为g[j]==i的所有的 a[j]中最大的
  17. 从后往前枚举i 然后维护 bg O(1)转移
  18. 上述过程可能 i和bg维护的j之间 他没有0 那就不能转移
  19. 所以 按0分段 遇到0 就把之前的信息更新bg
  20.  
  21. 然后没了
  22.  
  23. */
  24. #include<cstdio>
  25. #include<iostream>
  26. #include<cstdlib>
  27. #define lc k*2
  28. #define rc k*2+1
  29. #define mid (l+r)/2
  30. #define maxn 400010
  31. #define ll long long
  32. using namespace std;
  33. ll n,a[maxn],f[maxn],s[maxn],g[maxn],as[maxn],bg[maxn],c[maxn][];
  34. void Insert(ll k,ll l,ll r,ll x,ll y){
  35. if(x==l&&r==x){
  36. s[k]=max(s[k],y);return;
  37. }
  38. if(x<=mid)Insert(lc,l,mid,x,y);
  39. else Insert(rc,mid+,r,x,y);
  40. s[k]=max(s[lc],s[rc]);
  41. }
  42. ll Query(ll k,ll l,ll r,ll x,ll y){
  43. if(x>y)return ;
  44. if(x<=l&&y>=r)return s[k];
  45. ll res=;
  46. if(x<=mid)res=max(res,Query(lc,l,mid,x,y));
  47. if(y>mid)res=max(res,Query(rc,mid+,r,x,y));
  48. return res;
  49. }
  50. int main(){
  51. while(~scanf("%lld",&n)){
  52. for(ll i=;i<=n*;i++)
  53. s[i]=f[i]=g[i]=as[i]=;
  54. for(ll i=;i<=n;i++){
  55. scanf("%lld",&a[i]);
  56. //a[i]=rand();
  57. f[i]=;g[i]=;
  58. }
  59. ll mxx=;
  60. for(ll i=;i<=n;i++){
  61. if(a[i]==)continue;
  62. ll mx=Query(,,n,,a[i]-);
  63. f[i]=mx+;mxx=max(mxx,f[i]);
  64. Insert(,,n,a[i],f[i]);
  65. }
  66. for(ll i=;i<=n*;i++)s[i]=;
  67. for(ll i=n;i>=;i--){
  68. if(a[i]==)continue;
  69. ll mx=Query(,,n,a[i]+,n);
  70. g[i]=mx+;Insert(,,n,a[i],g[i]);
  71. }
  72. for(ll i=;i<=n*;i++)bg[i]=;
  73. ll cnt=;a[]=-;
  74. for(ll i=n;i>=;i--){
  75. if(a[i]==){
  76. for(ll j=;j<=cnt;j++)
  77. bg[c[j][]]=max(bg[c[j][]],c[j][]);
  78. cnt=;bg[]=n+;
  79. }
  80. else{
  81. ll mx=bg[mxx-f[i]];
  82. c[++cnt][]=g[i];c[cnt][]=a[i];
  83. if(mx-<a[i]+)continue;
  84. as[a[i]+]++;as[mx]--;
  85. }
  86. }
  87. ll ans=;
  88. for(ll i=;i<=n;i++)as[i]+=as[i-];
  89. for(ll i=;i<=n;i++){
  90. if(as[i]>)ans+=i*(mxx+);
  91. else ans+=i*mxx;
  92. //("%lld\n",ans);
  93. }
  94. printf("%lld\n",ans);
  95. }
  96. return ;
  97. }

Longest Increasing Subsequence HDU - 6284的更多相关文章

  1. 最长上升子序列 LIS(Longest Increasing Subsequence)

    引出: 问题描述:给出一个序列a1,a2,a3,a4,a5,a6,a7….an,求它的一个子序列(设为s1,s2,…sn),使得这个子序列满足这样的性质,s1<s2<s3<…< ...

  2. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  3. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  4. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  5. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  7. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

  8. 300. Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...

  9. SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

    Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...

随机推荐

  1. 最容易理解的HMM文章

    wiki上一个比较好的HMM例子 分类 隐马尔科夫模型 HMM(隐马尔科夫模型)是自然语言处理中的一个基本模型,用途比较广泛,如汉语分词.词性标注及语音识别等,在NLP中占有很重要的地位.网上关于HM ...

  2. JAVA 学习笔记 - 基础语法 2

    ---恢复内容开始--- 1.数组的申明和应用 数据类型 数组名称[] = null;             //在堆栈中申明变量名称 数组名称 =  new  数据类型[10];       // ...

  3. Python之字符串的特性及常用方法

    字符串的特性 索引: (索引是从0开始) s='hello'print(s[0])print(s[4])print(s[-1]) #拿出最后一个字符 hoo12345678截取s[start:stop ...

  4. token机制完成登录状态保持/身份认证

    一般APP都是刚安装后,第一次启动时需要登录(提示你需要登录或者直接启动在登录界面).而只要登录成功后,以后每次启动时都是登录状态,不需要每次启动时再次登录.不过,也有些APP若你长期未启动,再次启动 ...

  5. break和continue在多重循环中使用

    break和continue在多重循环中使用 关于break和continue在java中,break的作用是跳出循环,continue的作用是跳出本次循环. 我们一般情况下,这样使用: public ...

  6. Luogu P1991 无线通讯网

    P1991 无线通讯网 题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫 ...

  7. 以gnome-terminal为例,修改gnome3 的默认配置

    gnome连续几个版本的terminal默认配置文件都是同一个配置文件“b1dcc9dd-5262-4d8d-a863-c897e6d979b9”.这是因为gnome的developers编辑了这个配 ...

  8. 转载:SpringMVC的工作原理图

    SpringMVC的工作原理图: SpringMVC流程 1.  用户发送请求至前端控制器DispatcherServlet. 2.  DispatcherServlet收到请求调用HandlerMa ...

  9. Python,subprocess模块(补充)

    1.subprocess模块,前戏 res = os.system('dir') 打印到屏幕,res为0或非0 os.popen('dir') 返回一个内存对象,相当于文件流 a = os.popen ...

  10. git 忽略文件[.gitignore]常用配置

    .idea .buildpath .project .settings .Ds_Store composer.json composer.lock a.php /public/uploads /run ...