题目链接

题意

给出一个长度为\(n\)的序列\(a\),问有多少个区间\([l,r]\)满足:在区间\([l,r]\)内,\([1,r-l+1]\)的每个整数都恰好出现了一次。

\(n \le 3 \times 10 ^ 5\),\(a_i \le n\)

思路

可以发现,其实最后的答案一定不会很大。

所以:暴力出奇迹!!!

先对题意进行小小的转化,题目等价于问有多少个区间\([l,r]\)满足以下两个条件:

1.区间\([l,r]\)中的每个数字都只在区间\([l,r]\)中出现了一遍

2.\(max\{a_l,a_{l+1}...a_r\}=r-l + 1\)

首先只考虑条件一

从后往前扫这个序列。用\(nxt_i\)表示在满足每个数字只出现一遍的前提下,以i为左端,右端点最靠右的位置。(感性理解,我也不知道该咋表述了233.)换句话说,就是\([i,nxt_i - 1]\)这个区间是满足条件的,而\([i,nxt_i]\)是不满足条件的。用\(pos_i\)表示i这个数字上次出现的位置。那么就有\(nxt_i = min(nxt_{i+1},pos[a_i])\)

在上面的基础上,找满足第二个条件的区间

在当前区间左端点为l的情况下,右端点可以是\([l,nxt_l-1]\)。

直接枚举肯定爆炸。

从左到右枚举右端点r,

当找到满足条件的区间时,就把答案加上1。然后继续枚举

如果当前枚举的区间不符合条件时,也就是说\(l+max\{a_l,a_{l+1}...a_r\} > r\)时。那么从r到\(l+max\{a_l,a_{l+1}...a_r\}\)肯定也是不满足条件的,所以直接把\(r\)调到\(l+max\{a_l,a_{l+1}...a_r\}\)就行了。

然后就可以跑过去这道题了(似乎还蛮快的233)。

代码

/*
* @Author: wxyww
* @Date: 2019-06-06 15:53:44
* @Last Modified time: 2019-06-06 16:36:31
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 300000 + 100;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int tree[N << 2];
int a[N];
void build(int rt,int l,int r) {
if(l == r) {
tree[rt] = a[l];return;
}
int mid = (l + r) >> 1;
build(rt << 1,l,mid);
build(rt << 1 | 1,mid + 1,r);
tree[rt] = max(tree[rt << 1],tree[rt << 1 | 1]);
}
int query(int rt,int l,int r,int L,int R) {
if(L <= l && R >= r) return tree[rt];
int mid = (l + r) >> 1;
int ret = 0;
if(L <= mid) ret = max(ret,query(rt << 1,l,mid,L,R));
if(R > mid) ret = max(ret,query(rt << 1 | 1,mid + 1,r,L,R));
return ret;
}
int nxt[N],pos[N],n;
int main() {
n = read();
for(int i = 1;i <= n;++i) a[i] = read(),pos[i] = n + 1;
build(1,1,n);
int ans = 0;
nxt[n + 1] = n + 1;
for(int i = n;i >= 1;--i) {
nxt[i] = min(pos[a[i]],nxt[i + 1]);
pos[a[i]] = i;
for(int j = i;j < nxt[i];++j) {
int x = query(1,1,n,i,j);
if(i + x - 1 > j) j = i + x - 2;else ++ans;
}
}
cout<<ans;
return 0;
}

CF1175F The Number of Subpermutations的更多相关文章

  1. Codeforces 1175F The Number of Subpermutations

    做法①:RMQ(预处理NLOGN+后续跳跃蜜汁复杂度) 满足题意的区间的条件转换: 1.长度为R-L+1则最大值也为R-L+1 2.区间内的数不重复 当RMQ(L,R)!=R-L+1时 因为已经保证了 ...

  2. Codeforces 1175F The Number of Subpermutations (思维+rmq)

    题意: 求区间[l, r]是一个1~r-l+1的排列的区间个数 n<=3e5 思路: 如果[l,r]是一个排列,首先这里面的数应该各不相同,然后max(l,r)应该等于r-l+1,这就能唯一确定 ...

  3. Codeforces 1175F - The Number of Subpermutations(线段树+单调栈+双针/分治+启发式优化)

    Codeforces 题面传送门 & 洛谷题面传送门 由于这场的 G 是道毒瘤题,蒟蒻切不动就只好来把这场的 F 水掉了 看到这样的设问没人想到这道题吗?那我就来发篇线段树+单调栈的做法. 首 ...

  4. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  5. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  6. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  7. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  8. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  9. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

随机推荐

  1. 手把手教你使用gogs搭建git私有仓库

    本来想在 Github 上建一个私仓,但是发现只能设置 3 个贡献者. 国内的码云也只能设置 5 个. 无意间看到了使用 gogs 可以搭建私服,正好手头有空闲的服务器,于是开干! https://g ...

  2. mysql8

    解决navicat不能连接问题: grant all privileges on *.* to ‘root’@’%’;ALTER USER 'root'@'localhost' IDENTIFIED ...

  3. Mybatis中的association用法

    这篇文章我们将来学习一些 association 用法 表结构 DROP TABLE IF EXISTS `student`; CREATE TABLE `student` (  `id` int(1 ...

  4. CentOS 7.6 安装htop

    yum -y install epel-release.noarch yum -y install htop htop 上面左上角显示CPU.内存.交换区的使用情况,右边显示任务.负载.开机时间,下面 ...

  5. eclipse快速给表达式生成对应变量的快捷键

    这里记录下在Eclipse中快速给表达式生成对应变量的快捷键,有两种方式. [Ctrl + 2] 光标放在该表达式行的任意位置,按[Ctrl+2],会弹出提示,根据提示选择[F/L/R],就会自动生成 ...

  6. matplotlib的使用——pie(饼图)的使用

    在我们进行数据分析的时候需要对得出的数据进行可视化,因此我们需要引入第三方包来帮助我们进行可视化分析,在这里使用matplotlib 一.安装 使用指令[pip install matplotlib] ...

  7. C# 人民币大写金额转换

    /// <summary>       /// 转换人民币大小金额       /// </summary>       /// <param name="nu ...

  8. 读取txt文件内容,并按一定长度分页显示

    private List<string> SaveContentUpload(FileUpload file) { List<string> list_content = ne ...

  9. trailhead学习之 LWC for Aura Developers

    本篇查看https://trailhead.salesforce.com/content/learn/modules/lightning-web-components-for-aura-develop ...

  10. Python笔记:threading(多线程操作)

    Python的线程操作在旧版本中使用的是thread模块,在Python27和Python3中引入了threading模块,同时thread模块在Python3中改名为_thread模块,thread ...