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 ...
随机推荐
- ACM北大暑期课培训第四天
今天讲了几个高级搜索算法:A* ,迭代加深,Alpha-Beta剪枝 以及线段树 A*算法 启发式搜索算法(A算法) : 在BFS算法中,若对每个状态n都设定估价函数 f(n)=g(n)+h(n) ...
- esri mdb 数据库导入 到postgreSQL
需求: 项目升级,需要将esri的个人数据库(mdb格式)导入到开源数据库postgreSQL中. 思路: 使用fwtools工具导出到数据库中. 环境: windows+fwtools+postgr ...
- arthas 使用指导
arthas 阿尔萨斯 这种命令行的东西首先得知道 如何使用帮助,帮助文档最先开始用的,应该是可以在网上找到的官方文档 文档一:https://alibaba.github.io/arthas/ind ...
- Prometheus+Alertmanager+Grafana监控组件容器部署
直接上部署配置文件 docker-compose.yml version: '3' networks: monitor: driver: bridge services: prometheus: im ...
- Unity_Dungeonize 随机生成迷宫
本文对随机生成迷宫的实现思路进行记录,其作用在于为游戏过程提供随机性以及节省开发周期,下面是Dungeonize的结构 随机迷宫的生成主要包括几个阶段 1.生成房间体结构,为墙体,自定义房间,自定义物 ...
- Governing sand 贪心
题目链接:https://ac.nowcoder.com/acm/contest/887/C 题目描述 The Wow village is often hit by wind and sand,th ...
- kaggle预测房价的代码步骤
# -*- coding: utf-8 -*- """ Created on Sat Oct 20 14:03:05 2018 @author: 12958 " ...
- 关于neo4j初入门(2)
DELETE删除 删除节点及相关节点和关系. DELETE <node-name-list> DELETE <node1-name>,<node2-name>,&l ...
- JS中字符串切片
1.charAt 作用:根据索引值获取字符串 s1= "Hello world"; // 根据索引求字符 var myChar = s1.charAt(4); console.lo ...
- 如梦令编程语言发布 (RML)
如梦令编程语言是在Rebol语言核心语法的基础上,做了一些自以为是的修改而来.谨以此为Rebol语法的传承,略尽绵薄之力. 基本概念 如梦令语言分属Lisp语系,代码本身是一个层层嵌套的Token列表 ...