题意一个序列的LIS为MAX, 求连续子序列的LIS为MAX的个数。

先求出LIS,记录以a[i]结尾的LIS的长度,以及LIS起始位置(靠右的起始位置)。

然后线性扫一遍,,线段树与树状数组的差距还是蛮大的,,线段树900+MS,险些超时,而树状数组仅仅400+MS

代码里注释部分为线段树做法。

 #include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 1e5+;
int seg[maxn<<],pt[maxn<<],a[maxn],vec[maxn],idx,n;
int pos1[maxn],pos2[maxn];
void hash_()
{
sort(vec,vec+idx);
idx = unique(vec,vec+idx) - vec;
for (int i = ; i < n ;i++)
a[i] = lower_bound(vec,vec+idx,a[i]) - vec + ;
}
int ans,pp;
/*
void update(int l,int r,int pos,int x,int val,int s)
{
if (l == r)
{
if (val == seg[pos] && s > pt[pos])
pt[pos] = s;
if (val > seg[pos])
{
pt[pos] = s;
seg[pos] = val;
}
return ;
}
int mid = (l + r) >> 1;
if (x <= mid)
update(l,mid,pos<<1,x,val,s);
else
update(mid+1,r,pos<<1|1,x,val,s);
seg[pos] = max(seg[pos<<1],seg[pos<<1|1]);
if (seg[pos<<1] > seg[pos<<1|1])
pt[pos] =pt[pos<<1];
if (seg[pos<<1] < seg[pos<<1|1])
pt[pos] = pt[pos<<1|1];
if (seg[pos<<1] == seg[pos<<1|1])
pt[pos] = max(pt[pos<<1],pt[pos<<1|1]);
}
void query(int l,int r,int pos,int ua,int ub)
{
if (ub < ua)
return ;
if (ua <= l && ub >= r)
{
if (ans < seg[pos])
{
ans = seg[pos];
pp = pt[pos];
}
if (ans == seg[pos] && pp < pt[pos])
pp = pt[pos];
return ;
}
int mid = (l + r) >> 1;
if (ua <= mid)
query(l,mid,pos<<1,ua,ub);
if (ub > mid)
query(mid+1,r,pos<<1|1,ua,ub);
}*/
inline int lowbit (int x)
{
return x & -x;
}
int c[][maxn];
void add(int x,int d,int s)
{
while (x <= idx)
{
if (c[][x] < d)
{
c[][x] = d;
c[][x] = s;
}
if (c[][x] == d && c[][x] < s)
{
c[][x] = s;
}
x += lowbit(x);
}
}
void query(int x)
{
while (x)
{
if (ans < c[][x])
{
pp = c[][x];
ans = c[][x];
}
if (ans == c[][x] && pp < c[][x])
{
pp = c[][x];
}
x -= lowbit(x);
}
}
int dp[maxn];
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while (~scanf ("%d",&n))
{
//memset(seg,0,sizeof(seg));
//memset(pt,0,sizeof(pt));
memset(c,,sizeof(c));
idx = ;
for (int i = ; i < n; i++)
{
scanf("%d",a+i);
vec[idx++] = a[i];
}
hash_();
int LIS = ;
for (int i = ; i < n; i++)
{
ans = ;
//query(1,n,1,1,a[i]-1);
query(a[i]-);
dp[i] = ans + ;
if (ans == )
pos1[i] = i;
else
pos1[i] = pp;
// update(1,n,1,a[i],dp[i],pos1[i]);
add(a[i],dp[i],pos1[i]);
LIS = max(dp[i],LIS);
}
int pre = n;
for (int i = n -; i >= ; i--)
{
if (dp[i] != LIS)
continue;
pos2[i] = pre-;
pre = i;
}
ll cnt = ;
for (int i = ; i < n; i++)
{
if (dp[i] != LIS)
continue;
cnt += (ll)(pos1[i] + ) * (pos2[i]+-i);
}
printf("%I64d\n",cnt);
}
return ;
}

HDU5141--LIS again (LIS变形)的更多相关文章

  1. HDU5087 Revenge of LIS II (LIS变形)

    题目链接:pid=5087">http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意: 求第二长的最长递增序列的长度 分析: 用step[i ...

  2. HDU - 3564 Another LIS(LIS+线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意 给出1~n的插入顺序,要求每次插入之后的LIS 分析 首先用线段树还原出最终序列.因为插入的顺序是按 ...

  3. hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)

    链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...

  4. LuoguAT2827 LIS (LIS)

    裸题 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm ...

  5. P1481 魔族密码(LIS变形)

    题目描述(题目链接:https://www.luogu.org/problem/P1481) 风之子刚走进他的考场,就…… 花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花) 风之 ...

  6. LIS系列总结

    此篇博客总结常见的LIS模型变形的解法. ------------------------------------------------------------------- 〇.LIS的$O(Nl ...

  7. 最长回文子序列LCS,最长递增子序列LIS及相互联系

    最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...

  8. Codeforces 486E LIS of Sequence --树状数组求LIS

    题意: 一个序列可能有多个最长子序列,现在问每个元素是以下三个种类的哪一类: 1.不属于任何一个最长子序列 2.属于其中某些但不是全部最长子序列 3.属于全部最长子序列 解法: 我们先求出dp1[i] ...

  9. O(nlogn)LIS及LCS算法

    morestep学长出题,考验我们,第二题裸题但是数据范围令人无奈,考试失利之后,刻意去学习了下优化的算法 一.O(nlogn)的LIS(最长上升子序列) 设当前已经求出的最长上升子序列长度为len. ...

  10. UVa 10635 (LIS+二分) Prince and Princess

    题目的本意是求LCS,但由于每个序列的元素各不相同,所以将A序列重新编号{1,2,,,p+1},将B序列重新编号,分别为B中的元素在A中对应出现的位置(没有的话就是0). 在样例中就是A = {1 7 ...

随机推荐

  1. 二维码_encode与decode

    二维码encode和decode工具类 import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.Buffere ...

  2. Java虚拟机内存区域堆(heap)的管理

    在上一节中Java 出现内存溢出的定位以及解决方案 中对于Java虚拟机栈以及方法区的内存出现的异常以及处理方式进行了解析,由于Java虚拟机对于堆的管理十分复杂,并且Java虚拟机中最基本的内存区域 ...

  3. 10 Questions To Make Programming Interviews Less Expensive--reference

    Conducting Interview is not cheap and costs both time and money to a company. It take a lot of time ...

  4. STL之auto_ptr

    What's auto_ptr? The auto_ptr type is provided by the C++ standard library as a kind of a smart poin ...

  5. Linux最大文件打开数

    介绍 在Linux下有时会遇到Socket/File : Can't open so many files的问题.其实Linux是有文件句柄限制的,而且Linux默认一般都是1024(阿里云主机默认是 ...

  6. MVC项目,系统找不到指定的文件。(异常来自 HRESULT:0x80070002)

    今天在用Visual Studio新建MVC项目的时候,遇到错误 系统找不到指定的文件.(异常来自 HRESULT:0x80070002) 解决办法:工具--> 扩展和更新 -->联机(V ...

  7. dl标签和table标签

    dl标签定义了一个定义列表 <html> <body> <h2>一个定义列表:</h2> <dl>   <dt>计算机</ ...

  8. Jquery插件-Html5图片上传并裁剪

    /** * 图片裁剪 * @author yanglizhe * 2015/11/16 */ (function($){ /** * Drag */ var Drag={obj:null,init:f ...

  9. W3C小组宣布:HTML5标准制定完成

    近日,W3C小组宣布已经完成对HTML5标准以及Canvas 2D性能草案的制定,这就意味着开发人员将会有一个稳定的“计划和实施”目标. Web性能工作组已经推出W3C的两个版本建议草案. Navig ...

  10. Qt在VS2013或Qt Creator 中的控制台输出方式设置

    首先值得注意的是:在写程序的时候,项目保存路径不要涉及到中文,否则容易出错! 一.Qt在VS2013中的控制台输出方式: 注意:这里是而不是Qt Application. 然后直接点击finish即可 ...