[hdu6991]Increasing Subsequence
令$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的更多相关文章
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- LintCode-Longest Increasing Subsequence
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
- LCIS POJ 2172 Greatest Common Increasing Subsequence
题目传送门 题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列 分析:a[i] != b[j]: dp[i][j] = dp[i-1][j ...
- 300. Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...
随机推荐
- MySQL强人“锁”难《死磕MySQL系列 三》
系列文章 一.原来一条select语句在MySQL是这样执行的<死磕MySQL系列 一> 二.一生挚友redo log.binlog<死磕MySQL系列 二> 前言 最近数据库 ...
- 反调试--CRC检测
#include"CRC32.h" #include<Windows.h> #include<iostream> using namespace std; ...
- 10.11 HTTPS
没有HTTPS的抓包截图 HTTPS=HTTP + TLS/SSL https 实现过程如下 1.客户端发起HTTPS请求 rewrite www.baidu.com https://www.baid ...
- Apache ShardingSphere:由开源驱动的分布式数据库中间件生态
2021 年 7 月 21 日 2021 亚马逊云科技中国峰会现场,SphereEx 联合创始人.Apache ShardingSphere PMC 潘娟受邀参与此次峰会,以<Apache Sh ...
- python-docx处理Word必备工具
我的理解 为什么会用到python-docx,因为近段时间下载了大量网文,但格式都是html的,我个人习惯使用word处理文字,于是就想法设法把html文档转换为word,首先要考虑的问题就是从h ...
- spark性能优化(一)
本文内容说明 初始化配置给rdd和dataframe带来的影响 repartition的相关说明 cache&persist的相关说明 性能优化的说明建议以及实例 配置说明 spark:2.4 ...
- Mybatis一级缓存的锅
问题背景 项目开发中有一个树形数据结构,不像经典组织结构树.菜单级别树,我们这个树形结构是用户后期手动建立起来的关系.因此数据库表结构为两张表:数据记录表.记录关系表,通过业务规则限制,形成的树形结构 ...
- mysql join语句的执行流程是怎么样的
mysql join语句的执行流程是怎么样的 join语句是使用十分频繁的sql语句,同样结果的join语句,写法不同会有非常大的性能差距. select * from t1 straight_joi ...
- Go语言核心36讲(Go语言进阶技术四)--学习笔记
10 | 通道的基本操作 作为 Go 语言最有特色的数据类型,通道(channel)完全可以与 goroutine(也可称为 go 程)并驾齐驱,共同代表 Go 语言独有的并发编程模式和编程哲学. D ...
- [no_code]OCR表格处理——技术规格说明书
项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 技术规格说明书 我们在这个课程的目标是 远程协同工作,采用最新技术开发软件 这个作业在哪个具体方面 ...