Codeforces 1175F The Number of Subpermutations (思维+rmq)
题意:
求区间[l, r]是一个1~r-l+1的排列的区间个数
n<=3e5
思路:
如果[l,r]是一个排列,首先这里面的数应该各不相同,然后max(l,r)应该等于r-l+1,这就能唯一确定这个区间满足条件了
我们只需要预处理出对于每个左端点,它能伸延到的最远的r,使得l到r各不相同,然后暴力
注意如果对于一次暴力不满足max(l,r)==r-l+1,那么此时r应该跳到l+max(r-l+1)-1,因为在这里才至少可能会满足条件
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0);
int a[maxn];
int d[maxn][];
int n;
void init(){
for(int i = ; i <= n; i++) d[i][] =a[i];
for(int j = ; (<<j) <= n; j++){
for(int i = ; i + (<<j) - <= n; i++){
d[i][j] = max(d[i][j-], d[i + (<<(j-))][j-]);
}
}
}
int rmq(int l, int r){
int k = ;
while((<<(k+)) <= r-l+)k++;
return max(d[l][k], d[r-(<<k)+][k]);
}
int mx[maxn];
int id[maxn];
int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
}
mx[n+]=n;
for(int i = n; i >= ; i--){
if(id[a[i]])mx[i]=min(mx[i+],id[a[i]]-);
else mx[i]=mx[i+];
id[a[i]]=i;
}
init();
int ans=;
for(int i = ; i <= n; i++){
int j = i;
while(j<=mx[i]){
//printf("%d %d %d\n",i,j,rmq(i,j));
if(rmq(i,j)==j-i+){ans++;j++;}
else j=i+rmq(i,j)-;
}
}
printf("%d",ans);
return ;
}
Codeforces 1175F The Number of Subpermutations (思维+rmq)的更多相关文章
- Codeforces 1175F The Number of Subpermutations
做法①:RMQ(预处理NLOGN+后续跳跃蜜汁复杂度) 满足题意的区间的条件转换: 1.长度为R-L+1则最大值也为R-L+1 2.区间内的数不重复 当RMQ(L,R)!=R-L+1时 因为已经保证了 ...
- Codeforces 1175F - The Number of Subpermutations(线段树+单调栈+双针/分治+启发式优化)
Codeforces 题面传送门 & 洛谷题面传送门 由于这场的 G 是道毒瘤题,蒟蒻切不动就只好来把这场的 F 水掉了 看到这样的设问没人想到这道题吗?那我就来发篇线段树+单调栈的做法. 首 ...
- Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...
- Codeforces Round #619 (Div. 2)E思维+二维RMQ
题:https://codeforces.com/contest/1301/problem/E 题意:给个n*m的图形,q个询问,每次询问问询问区间最大的合法logo的面积是多少 分析:由于logo是 ...
- Codeforces 55D Beautiful Number (数位统计)
把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容: a positive integer number is beautiful if and only if it is ...
- Codeforces Round #143 (Div. 2) (ABCD 思维场)
题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...
- Codeforces 514 D R2D2 and Droid Army(RMQ+二分法)
An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., am, w ...
- Codeforces Round #395 (Div. 2)(A.思维,B,水)
A. Taymyr is calling you time limit per test:1 second memory limit per test:256 megabytes input:stan ...
- Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
A. Vladik and Courtesy time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
随机推荐
- SQL练习题(一)
目录 题目一:交换性别(简单) 实现思路 提交代码 题目二:连续出现的数字(中等) 实现思路 方式一 方式二 提交代码 方式一 方式二 题目三:换座位(中等) 实现思路 方式一 方式二 提交代码 方式 ...
- jSignature签字板保存为图片
这是本人的第一篇博客,还不会使用.有些简陋,勿怪! 今天要讲的是使用jquery插件jSignature做一个手写板签字的功能,并将签字笔迹保存为图片. 第一步:环境准备 jquery.jSignat ...
- ocx控件的坑
前言 这还是第一次写博客,以前太懒了,现在发现是很有必要记录下这些经验和问题的.最近项目中有个需求(报表单据需要客户签名,连接签字板,把签名单据同步到服务器上),需要和硬件交互,当时硬件商提供了ocx ...
- 【转】你应该关注的几个Eclipse超酷插件
本文由 ImportNew - 唐尤华 翻译自 Anton Arhipov.如需转载本文,请先参见文章末尾处的转载要求. 来自非营利性Eclipse基金会的Eclipse IDE以其插件生态系统著称. ...
- GPU图形绘制管线简介
(阅读GPU+编程与CG+语言之阳春白雪下里巴人所得总结) GPU图形绘制管线是描述GPU渲染(把三维世界显示为屏幕上的二维图像)的流程,主要分为三个主要阶段应用程序阶段.几何阶段.光栅阶段. 1.应 ...
- 【开源】后台权限管理系统升级到aspnetcore3.1
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...
- 头条面试竟然问我maven
maven package和maven install 有什么区别? 你常用的maven命令有哪些? <dependencyManagement> 是干什么的? 还有用过其它构建工具吗? ...
- 逆元(inv)
推荐博客 : http://blog.csdn.net/baidu_35643793/article/details/75268911 通常我们在计算除法取模时,并不能直接的取模后再去相除,答案会有问 ...
- numpy基本知识
"""np.arrayobject 数组或嵌套的数列dtype 数组元素的数据类型,可选copy 对象是否需要复制,可选order 创建数组的样式,C为行方向,F为列方向 ...
- Scrapy去重
一.原生 1.模块 from scrapy.dupefilters import RFPDupeFilter 2.RFPDupeFilter方法 a.request_seen 核心:爬虫每执行一次yi ...