题意一个序列的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. 第二章:开始开发mod前你需要知道的一些事情

    <基于1.8 Forge的Minecraft mod制作经验分享> 是的童鞋,别着急.不管我写的再认真也不可能面面俱到,那么如果遇到了什么问题怎么办呢?所以咱必须先掌握一些基础的东西,这样 ...

  2. mui实现退出当前应用

    var first = null; var showMenu = false; mui.back = function() { if(showMenu) { closeMenu();} else { ...

  3. NuGet学习笔记(1)——初识NuGet及快速安装使用(转)

    关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...

  4. ASP.NET-FineUI开发实践-8(二)

    把上回的做一些改进 1.点击grid2的行改变TriggerBox1的值 var v = $(item).find('.x-grid-cell-Name div.x-grid-cell-inner') ...

  5. HTML基本概念

    什么是 HTML? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言,而是一种标记语言 (ma ...

  6. php 读xml的两种方式

    <?xml version="1.0" encoding="ISO-8859-1"?> <st> <stu> <nam ...

  7. maven部署命令

    参考文档:http://blog.csdn.net/woshixuye/article/details/8133050 http://www.blogjava.net/itvincent/archiv ...

  8. nginx添加缓存

    nginx的具体逻辑是什么样的? 分布式session spring session redis过滤器 有4种方案: 一直访问一台 //如果这台机器垮掉了,怎么办? session同步 序列化传输 / ...

  9. Java直接插入算法

    直接插入算法是将N个带排序的元素看做成一个有序表和一个无序表. 每次从无序表中取一个元素和有序表比较,重复N-1次完成排序. 直接上代码: package test; public class Tes ...

  10. web项目环境搭建(1):建立一个maven项目

    一.maven简介以及常用概念 1.Maven是一个项目管理和整合的工具.Maven为开发者提供了一套完整的构建生命周期框架.开发团队基本不用花多少时间就能自动完成工程的基础构建配置,因为Maven使 ...