题意一个序列的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. ApiDemos--&gt;Views-lists-slow adapter学习

    今天来依照apidemos提供的方法来实现slow loading的效果. 简单说下实现方法: 实现ListView.OnScrollListener ,监听到手势滑动的情况,当处于滚动状态时,将新显 ...

  2. fork 和 vfork 的区别与联系

    vfork用于创建一个新进程,而该新进程的目的是exec一个新进程,vfork和fork一样都创建一个子进程,但是它并不将父进程的地址空间完全复制到子进程中,不会复制页表.因为子进程会立即调用exec ...

  3. 通过模拟器和ida搭建Android动态调试环境的问题

    这几天在学Android的native层逆向.在按照教程用ida搭建动态调试环境时,第一步是把android_server 放到手机里执行,但是在手机里可以,在genymotion模拟器上就提示 no ...

  4. JAVA File转Byte[]

    /** * 获得指定文件的byte数组 */ public static byte[] getBytes(String filePath){ byte[] buffer = null; try { F ...

  5. 腾讯云升级到PHP7

    1.删除之前安装的PHP yum remove php* php-common 2.安装yum源 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel ...

  6. 创建实体数据模型需要注意的,不要选单复数形式,否则AddObject出问题

    //这个测试太不容易了,总是出错,addInfo 方法进去,最后调用context对象.AddObject(),也就是context.AddObject(entitySetName, entity); ...

  7. nginx环境下搭建nagios 3.5.0,及配置pnp4nagios画图

    本文基于<LNMP最新源码安装脚本>,Nagios依赖PHP环境和perl环境,由于Nginx不支持Perl的CGI,需先来搭建Perl环境,Nagios原理介绍略.一.下载最新稳定源码包 ...

  8. Property type 'id<tabBarDelegate>' is incompatible with type 'id<UITabBarDelegate> _Nullable' inherited from 'UITabBar'

    iOS报错:Property type 'id' is incompatible with type 'id _Nullable' inherited from 'UITabBar' 如图: 可能原因 ...

  9. C/C++中unsigned char和char的区别

    代码: #include <cstdio> #include <iostream> using namespace std; int main(){ unsigned char ...

  10. java学习笔记(12) —— Struts2 通过 xml /json 实现简单的业务处理

    XML 1.引入dom4j-2.0.0.jar 2.引入jquery-1.8.2.js 3.新建common.js getInfo = function(){ $.post("getXmlA ...