令$f_{i}$​​表示以$i$​​为结尾的极长上升子序列个数,则有$f_{i}=\sum_{j<i,a_{j}<a_{i},\forall j<k<i,a_{k}\not\in [a_{j},a_{i}]}f_{j}$

(初始状态为前缀最小值处$f_{i}=1$,最终答案为后缀最大值处的$f_{i}$​之和)

暴力计算复杂度显然为$o(n^{2})$,无法通过

考虑分治计算,当递归到区间$[l,r]$时,需要求出仅考虑$[l,r]$内部的(包括转移的$j$)时的$f_{i}$

具体的,先递归$[l,mid]$,再求出$[l,mid]$对$(mid,r]$的影响,最后递归$(mid,r]$即可

第一步和第三步容易处理,接下来考虑第二步:

具体的,考虑将$a_{l},a_{l+1},...,a_{r}$从小到大排序后枚举,注意到此时左侧的数中,如果存在$x<y$且$a_{x}<a_{y}$,那么$x$一定不会被使用(因为之后右侧的$a_{i}>a_{y}$​​),也即可以维护一个单调栈

(关于这个单调栈,从栈底到栈顶位置单调递减、权值单调递增)

类似地,我们再对右侧维护一个单调栈,从栈底到栈顶位置和权值都单调递增,此时即查询比左边单调栈中比当前比右边单调栈栈顶(插入前,若为空则定义为0)大的位置的$f$之和,可以二分实现

由于需要排序和二分,总复杂度为$o(n\log^{2}n)$,可以通过

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 100005
4 #define mod 998244353
5 int t,n,ans,a[N],id[N],stl[N],str[N],sum[N],f[N];
6 bool cmp(int x,int y){
7 return a[x]<a[y];
8 }
9 void calc(int l,int r){
10 if (l==r)return;
11 int mid=(l+r>>1);
12 calc(l,mid);
13 for(int i=l;i<=r;i++)id[i]=i;
14 sort(id+l,id+r+1,cmp);
15 stl[0]=str[0]=0;
16 for(int i=l;i<=r;i++){
17 if (id[i]<=mid){
18 while ((stl[0])&&(stl[stl[0]]<id[i]))stl[0]--;
19 stl[++stl[0]]=id[i];
20 sum[stl[0]]=(sum[stl[0]-1]+f[id[i]])%mod;
21 }
22 else{
23 while ((str[0])&&(str[str[0]]>id[i]))str[0]--;
24 int pos=lower_bound(stl+1,stl+stl[0]+1,str[str[0]],cmp)-stl;
25 str[++str[0]]=id[i];
26 f[id[i]]=(f[id[i]]+(sum[stl[0]]-sum[pos-1]+mod)%mod)%mod;
27 }
28 }
29 calc(mid+1,r);
30 }
31 int main(){
32 scanf("%d",&t);
33 while (t--){
34 scanf("%d",&n);
35 for(int i=1;i<=n;i++)scanf("%d",&a[i]);
36 int s=n+1;
37 for(int i=1;i<=n;i++){
38 f[i]=(a[i]<s);
39 s=min(s,a[i]);
40 }
41 calc(1,n);
42 s=ans=0;
43 for(int i=n;i;i--){
44 if (a[i]>s)ans=(ans+f[i])%mod;
45 s=max(s,a[i]);
46 }
47 printf("%d\n",ans);
48 }
49 return 0;
50 }

[hdu6991]Increasing Subsequence的更多相关文章

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

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

  2. [tem]Longest Increasing Subsequence(LIS)

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

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

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

  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. LCIS POJ 2172 Greatest Common Increasing Subsequence

    题目传送门 题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列 分析:a[i] != b[j]: dp[i][j] = dp[i-1][j ...

  9. 300. Longest Increasing Subsequence

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

随机推荐

  1. Codeforces Round #747 (Div. 2)

    比赛地址 A(水题) 题目链接 题目: 给出指定\(n\),求解出一段区间\([l,r]\)使得\(\sum\limits_{i=l}^ri=n\) 解析: 从点0,1两点作为起点分别向左右延伸长度, ...

  2. 题解 「BZOJ2137」submultiple

    题目传送门 题目大意 给出 \(M,k\) ,求出 \[\sum_{x|M}\sigma(x)^k \] 给出 \(P_i\),满足 \(n=\prod_{i=1}^{n}a_i^{P_i}\),其中 ...

  3. nginx搭建网站踩坑经历

    为了更好的阅读体验,请访问我的个人博客 前言 早上刷抖音刷到一个只需要三步的nginx搭建教程(视频地址),觉得有些离谱,跟着复现了一遍,果然很多地方不严谨并且省略了大量步骤,对于很多不了解linux ...

  4. 详解python三大器——迭代器、生成器、装饰器

    迭代器 聊迭代器前我们要先清楚迭代的概念:通常来讲从一个对象中依次取出数据,这个过程叫做遍历,这个手段称为迭代(重复执行某一段代码块,并将每一次迭代得到的结果作为下一次迭代的初始值). 可迭代对象(i ...

  5. csh

    在*unix系统中,常用的shell有sh,bash,csh/tcsh, ksh.  sh来自于systemV的Unix,是传统的Unix的shell,直到现在很多的系统管理员仍然喜欢使用sh. ba ...

  6. css实现水平-垂直居中的方法

    * 定宽居中: 1.absolute+负margin 2.absolute+margin:auto 3.absolute--calc 4.min-height:100vh + flex + margi ...

  7. 吴恩达课后习题第二课第三周:TensorFlow Introduction

    目录 第二课第三周:TensorFlow Introduction Introduction to TensorFlow 1 - Packages 1.1 - Checking TensorFlow ...

  8. JVM:类加载与字节码技术-2

    JVM:类加载与字节码技术-2 说明:这是看了 bilibili 上 黑马程序员 的课程 JVM完整教程 后做的笔记 内容 这部分内容在上一篇笔记中: 类文件结构 字节码指令 编译期处理 类加载阶段 ...

  9. [对对子队]Beta阶段项目展示博客

    Beta阶段项目展示博客 1 团队成员的简介和个人博客地址 成员 头像 岗位 博客 个人介绍 黄贤昊 PM 17373253 喜欢玩游戏和做游戏,项目经验基本都和游戏相关,擅长摸鱼,偶尔敬业. 吴桐雨 ...

  10. Beta阶段第十次会议

    Beta阶段第十次会议 时间:2020.5.26 完成工作 姓名 完成工作 难度 完成度 ltx 1.修正小程序新闻bug2.修正小程序认证bug 中 80% xyq 1.上传信息编辑部分代码到服务器 ...