[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 ...
随机推荐
- javascriptRemke之深入迭代
javascriptRemke之深入迭代 前言:"迭代"意为按照顺序反复多次执行一段程序,ECMAscript6中新增了两个高级特性:迭代器与生成器,使用这两个特性能更高效地实现迭 ...
- linux kill信号详解
大家对kill -9 肯定非常熟悉,在工作中也经常用到.特别是你去重启tomcat时.可是多半看来,我们对-9的理解只是表面而已. 很少有人(包括我)认真的去了解一下 kill -n 这个n到底是什么 ...
- Appium iOS 原理
一.iOS Appium 原理 1.1 iOS 9.3 系统之前自动化测试 1.1.1 Native 自动化 这是 iOS 9.3 系统之前自动化测试的架构模式.通过 Android Appium 原 ...
- [NOIP2013 提高组] 华容道 P1979 洛谷
[NOIP2013 提高组] 华容道 P1979 洛谷 强烈推荐,更好的阅读体验 经典题目:spfa+bfs+转化 题目大意: 给出一个01网格图,和点坐标x,y空格坐标a,b,目标位置tx,ty要求 ...
- PAT (Basic Level) Practice (中文)1014 福尔摩斯的约会 (20分)
1014 福尔摩斯的约会 (20分) 带侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hys ...
- 从 MVC 到使用 ASP.NET Core 6.0 的最小 API
从 MVC 到使用 ASP.NET Core 6.0 的最小 API https://benfoster.io/blog/mvc-to-minimal-apis-aspnet-6/ 2007 年,随着 ...
- .Net微信服务商平台ApiV3接口
最近做个对接微信服务商平台的小程序项目,大概要实现的流程是:a)特约商户进件 > b)生成带参数的小程序码 > c)小程序支付 > d)分账,记录一下,希望能对需要的朋友有所帮助 开 ...
- git GUI Clients
git GUI Clients Git 自带用于提交 (git-gui) 和浏览 (gitk) 的内置 GUI 工具,但也有一些第三方工具供寻求特定平台体验的用户使用. References Git ...
- Zookeeper+Dubbo环境搭建与Demo测试
环境准备: 1. zookeeper-3.4.14 (下载地址:http://archive.apache.org/dist/zookeeper/) 2. dubbo-0.2.0 (下载地址 ...
- 初始CSS01
CSS基础知识 CSS介绍 CSS全称为层叠样式表,与HTML相辅相成,实现网页的排版布局与样式美化. 使用方式 根据样式表在页面中呈现的方式不同,可以通过以下三种方式在页面中使用格式 内联样式 改样 ...