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 ...
随机推荐
- Python在Windows下列出所有的安装包和模块
1.查看python安装的module python -m pydoc module 或 >>>help('module') 2.用pip查看 pip list
- 【记】Linux下安装JDK1.7
Java官网已经不提供除最新版本以外版本的JDK下载了,下载JDK1.7,密码: rsqg 本地Linux系统为Centos6.9,本身就没安装Java:已安装Java需要先卸载,卸载方法请百度. 1 ...
- Spring工程报错
错误日志: 2014-09-24 10:50:16 [org.springframework.context.support.FileSystemXmlApplicationContext]-[INF ...
- TensorFlow——卷积神经网络的相关函数
在TensorFlow中,使用tr.nn.conv2d来实现卷积操作,使用tf.nn.max_pool进行最大池化操作.通过闯传入不同的参数,来实现各种不同类型的卷积与池化操作. 卷积函数tf.nn. ...
- Spring学习记录3——Spring AOP
SpringAOP基础 AOP简介: AOP是Aspect Oriented Programing的简称,翻译为“面向切面编程”.它适用于具有横切逻辑的应用场合,如性能检测,访问控制,事务管理及日志记 ...
- CF - 高精度 + 贪心
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two foll ...
- minikube 设置CPU和内存
安装minikube之后,第一次sudo minikube start 时,设置参数--cpus int --memory int . 如果需要更改设置,需要将缓存文件$HOME/.minikube ...
- ORM基础4 跨表查询+原子性操作
一.跨表查询 1.# # 正向查找 对象查找 # book_obj = models.Book.objects.get(id=3) # print(book_obj) # ret = book_obj ...
- ZooKeeper 并不适合做注册中心
zookeeper 的 CP 模型不适合注册中心 zookeeper 是一个非常优秀的项目,非常成熟,被大量的团队使用,但对于服务发现来讲,zookeeper 真的是一个错误的方案. 在 CAP 模型 ...
- 异数OS 星星之火(一)-- 异数OS-织梦师云 用户使用手册
. 异数OS 星星之火(一)– 异数OS-织梦师云 用户使用手册 本文来自异数OS社区 github: https://github.com/yds086/HereticOS 异数OS社区QQ群: 6 ...