令$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. Springboot中使用Redisson实现分布式锁

    1. 概述 老话说的好:便宜没好货,有价值的商品,即使再贵,也有人会买. 言归正传,今天继续讨论有关"锁"的话题,synchronized 和 ReentrantLock 大家应该 ...

  2. Python | 一键生成九宫格图片

    一键生成九宫格图片 首先我们准备几张图片: 将代码文件放在放置图片的地方,用软件打开: 点击运行,在当前目录下会生成一个文件夹: 打开新生成的文件夹: 打开对应图片的名称文件夹: 如果不想图片被分成9 ...

  3. (googlechrome)未知错误导致安装失败,如果googlechrome....

    ​https://jingyan.baidu.com/article/ea24bc39ffb699da63b33147.html#5827690-tsina-1-63512-fe183374908e7 ...

  4. 题解 CF961G 【Partitions】

    题目传送门 题目大意 给出\(n,k\),以及\(w_{1,2,..,n}\),定义一个集合\(S\)的权值\(W(S)=|S|\sum_{x\in S} w_x\),定义一个划分\(R\)的权值为\ ...

  5. selenium 4.0 发布

    我们非常高兴地宣布Selenium 4的发布.这适用于Java..net.Python.Ruby和Javascript.你可以从你最喜欢的包管理器或GitHub下载它! https://github. ...

  6. C#并行编程:Parallel的使用

    前言:在C#的System.Threading.Tasks 命名空间中有一个静态的并行类:Parallel,封装了Task的使用,对于执行大量任务提供了非常简便的操作.下面对他的使用进行介绍. 本篇内 ...

  7. find+xargs+sed批量替换

    写代码时经常遇到要把 .c  和 .h的文件中的某些内容全部替换的情况,用sourceinsight 进行全局的查找是一个方法,但是sourceinsight只能替换一个文件中的字符串,不能同时替换多 ...

  8. QQ消算轰炸,我好无聊真的

    from pynput.keyboard import Key,Controller import time from random import choice time.sleep(5) # 创建键 ...

  9. xshell几款绝佳配色方案

    NO.1 [mycolor] text(bold)=e9e9e9 magenta(bold)=ff00ff text=00ff80 white(bold)=fdf6e3 green=80ff00 re ...

  10. OO前三次作业思考(第一次OO——Blog)

    OO前三次作业总结 基于度量分析程序结构 由于三次作业较多,决定分析内容.功能最为复杂的第三次作业. 上图为第三次作业的类图.我使用了一个抽象类Factor,写了五个因子继承Factor,然后又单独开 ...