/*
首先预处理好f g数组
fi :以a[i]为结尾的 最长上升子序列的长度
gi :以a[i]为开始的 最长上升子序列的长度
mxx : 最长上升子序列的长度
线段树优化 nlogn
(不包含a[i]==0) 显然把所有0换成x 只可能是mxx变成mxx+1 然后我们考虑一对 i j (下标)
若 f[i]+g[j]==mxx 则 所有a[i]+1~~~a[j]-1之间的x
他们对用的lis长度为mxx+1
然后枚举i j凉了
对于一个i 我们只需要找到 他后面的 一个j 满足 f[i]+g[j]==mxx 并且a[j]最大
然后维护bg[i] 表示长度为g[j]==i的所有的 a[j]中最大的
从后往前枚举i 然后维护 bg O(1)转移
上述过程可能 i和bg维护的j之间 他没有0 那就不能转移
所以 按0分段 遇到0 就把之前的信息更新bg 然后没了 */
#include<cstdio>
#include<iostream>
#include<cstdlib>
#define lc k*2
#define rc k*2+1
#define mid (l+r)/2
#define maxn 400010
#define ll long long
using namespace std;
ll n,a[maxn],f[maxn],s[maxn],g[maxn],as[maxn],bg[maxn],c[maxn][];
void Insert(ll k,ll l,ll r,ll x,ll y){
if(x==l&&r==x){
s[k]=max(s[k],y);return;
}
if(x<=mid)Insert(lc,l,mid,x,y);
else Insert(rc,mid+,r,x,y);
s[k]=max(s[lc],s[rc]);
}
ll Query(ll k,ll l,ll r,ll x,ll y){
if(x>y)return ;
if(x<=l&&y>=r)return s[k];
ll res=;
if(x<=mid)res=max(res,Query(lc,l,mid,x,y));
if(y>mid)res=max(res,Query(rc,mid+,r,x,y));
return res;
}
int main(){
while(~scanf("%lld",&n)){
for(ll i=;i<=n*;i++)
s[i]=f[i]=g[i]=as[i]=;
for(ll i=;i<=n;i++){
scanf("%lld",&a[i]);
//a[i]=rand();
f[i]=;g[i]=;
}
ll mxx=;
for(ll i=;i<=n;i++){
if(a[i]==)continue;
ll mx=Query(,,n,,a[i]-);
f[i]=mx+;mxx=max(mxx,f[i]);
Insert(,,n,a[i],f[i]);
}
for(ll i=;i<=n*;i++)s[i]=;
for(ll i=n;i>=;i--){
if(a[i]==)continue;
ll mx=Query(,,n,a[i]+,n);
g[i]=mx+;Insert(,,n,a[i],g[i]);
}
for(ll i=;i<=n*;i++)bg[i]=;
ll cnt=;a[]=-;
for(ll i=n;i>=;i--){
if(a[i]==){
for(ll j=;j<=cnt;j++)
bg[c[j][]]=max(bg[c[j][]],c[j][]);
cnt=;bg[]=n+;
}
else{
ll mx=bg[mxx-f[i]];
c[++cnt][]=g[i];c[cnt][]=a[i];
if(mx-<a[i]+)continue;
as[a[i]+]++;as[mx]--;
}
}
ll ans=;
for(ll i=;i<=n;i++)as[i]+=as[i-];
for(ll i=;i<=n;i++){
if(as[i]>)ans+=i*(mxx+);
else ans+=i*mxx;
//("%lld\n",ans);
}
printf("%lld\n",ans);
}
return ;
}

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. Array与NSArray关系

    NSArray与Array之间的关系如同NSString与String之间的关系,NSArray是类类型,而Array是结构体类型,一个是引用类型,一个是值类型,它们是怎样实现无缝转换的呢?Swift ...

  2. JS模拟CSS3动画-贝塞尔曲线

    一.什么是贝塞尔曲线 1962年,法国工程师皮埃尔·贝塞尔(Pierre Bézier),贝塞尔曲线来为为解决汽车的主体的设计问题而发明了贝塞尔曲线.如今,贝赛尔曲线是计算机图形学中相当重要的一种曲线 ...

  3. arx 插入图片

    #include <ShLwApi.h> #pragma comment(lib, "ShLwApi.lib") //插入影像图 Acad::ErrorStatus i ...

  4. Mybatis学习总结二

    Mapper动态代理开发方式 实现原理: Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象. Mapper接口开发需 ...

  5. 16位/32位/64位CPU的位究竟是说啥

    平时,我们谈论CPU,都会说某程序是32位编译,可以跑在32位机或64位机,或则是在下载某些开源包时,也分32位CPU版本或64CPU位版本,又或者在看计算机组成相关书籍时,特别时谈到X86 CPU时 ...

  6. 12Oracle Database SQL程序

    Oracle Databse SQL 程序 程序块 在这之前,我们所有的sql语句都是一句一句执行的,如果我们把很多事情看作一个整体提交执行的话,必须使用程序块. 声明部分:声名变量及初始化 关键字: ...

  7. 【转载】appium自动化环境搭建

    1.java开发环境JDK 2.android SDK(platform/platform tools/tools/build tools) 3.python下载安装(pip) 4.appium下载安 ...

  8. MyBatis 的基本介绍及使用

    一.简介 ​ MyBatis 是支持定制化 SQL.存储过程以及高级映射的持久层框架(ORM).MyBatis 可以使用简单的 XML 或 注解用于配置和映射数据表,是将 POJO(Plain Old ...

  9. UVA - 1620 Lazy Susan(逆序数)

    题目: 把1~n(n≤500)放到一个圆盘里,每个数恰好出现一次.每次可以选4个连续的数字翻转顺序.问能不能变成1.2.3....n的顺序. 思路: 这样的题的规律真的是一点都不好推,看了网上的博客知 ...

  10. python下载网络文件

    python下载网络文件 制作人:全心全意 下载图片 #!/usr/bin/python #-*- coding: utf-8 -*- import requests url = "http ...