令$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. CF125E MST company (凸优化+MST)

    qwq自闭的一个题 我来修锅辣!!!!!! 这篇题解!可以\(hack\)全网大部分的做法!!! 首先,我们可以把原图中的边,分成两类,一类是与\(1\)相连,另一类是不与\(1\)相连. 原题就转化 ...

  2. bzoj3073Journeys(线段树优化最短路)

    这里还是一道涉及到区间连边的问题. 如果暴力去做,那么就会爆炸 那么这时候就需要线段树来优化了. 因为是双向边 所以需要两颗线段树来分别对应入边和出边 QwQ然后做就好了咯 不过需要注意的是,这个边数 ...

  3. 痞子衡嵌入式:超级下载算法RT-UFL v1.0在恩智浦MCUXpresso IDE下的使用

    痞子衡主导的"学术"项目 <RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计> v1.0 版发布近 4 个月了,部分客户已经在实际项目开发调试中用上了这个 ...

  4. 初次认识指针:C语言*p、p以及&p的区别,*p和**p的区别?

    https://blog.csdn.net/weixin_43115440/article/details/93475460 先要理解地址和数据,你可以想象有很多盒子,每个盒子有对应的号码,那个号码叫 ...

  5. SharkCTF2021 easy_phpserialize题记

    ***先说教训: (1)不要看到正则就走不动路:有些正则不一定能绕. (2)__wakeup()漏洞在php5.6以上就被修复了: 本地复现各种题目时要注意环境. -------- 扫描,得到inde ...

  6. 关于takin-data,你想知道的都在这里(二)trace日志篇

    相信大家在使用takin的过程中都见到过压测过程中实时展示的请求流量明细和请求详情了吧,像这样: 还有这样: 这样的请求流量明细和调用链详情是怎么实现的呢,今天就带大家探究下. 在前面的启动命令篇(h ...

  7. Java:创建对象小记

    Java:创建对象小记 对 Java 中的创建对象的内容,做一个微不足道的小小小小记 创建对象的方式概述 使用 new 关键字:Person person = new Person(); 反射创建:使 ...

  8. [对对子队]会议记录4.14(Scrum Meeting 5)

    今天已完成的工作 刘子航 ​ 工作内容:设计第2,3关 ​ 相关issue:设计关卡2,3 吴昭邦 ​ 工作内容:制作场景,暂时解决了坐标错位问题 ​ 相关issue:实现游戏场景中的必要模型 何瑞 ...

  9. linux centos7 修改默认网卡命名规则为eth0脚本

    CentOS6之前基于传统的命名方式如:eth1,eth0.... Centos7提供了不同的命名规则,默认是基于固件.拓扑.位置信息来分配.这样做的优点是命名是全自动的.可预知的,缺点是比eth0. ...

  10. Spring Cloud Alibaba 介绍及工程准备

    简介 SpringCloud Alibaba是阿里巴巴集团开源的一套微服务架构解决方案. 微服务架构是为了更好的分布式系统开发,将一个应用拆分成多个子应用,每一个服务都是可以独立运行的子工程.其中涵盖 ...