题目链接

题意

给出一个长度为\(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. 记录下github 与 gitee 同时使用

    参考 Gitee(码云).Github同时配置ssh key 中间有一步,创建config文件,然后测试就过不去了. 报错:Bad owner or permissions on C:\\Users\ ...

  2. webpack打包教程(一)常用loader详解

    1.打包图片 // { // test: /\.(png|jpe?g|gif)$/i, // use: [{ // loader: 'file-loader', // options: { // na ...

  3. CentOS7安装Oracle 11g数据库

    转载:https://blog.csdn.net/lia17/article/details/82256565 rpm -ivh --force --nodeps *.rpm 强制装 rpm依赖包下载 ...

  4. vuex 源码解析(四) mutation 详解

    mutation是更改Vuex的store中的状态的唯一方法,mutation类似于事件注册,每个mutation都可以带两个参数,如下: state ;当前命名空间对应的state payload ...

  5. 一个简单的利用 WebClient 异步下载的示例(三)

    继续上一篇 一个简单的利用 WebClient 异步下载的示例(二) 后,继续优化它. 1. 直接贴代码了: DownloadEntry: public class DownloadEntry { p ...

  6. (译)Kubernetes中的多容器Pod和Pod内容器间通信

    原文:https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/Pave ...

  7. F#周报2019年第22期

    新闻 2019年实用F#挑战结果 FSharp.Formatting正在确定维护者 实用F#挑战的赢家们 使用F#在分布式系统中进行故障检测与共识 F#里的Cloudflare Worker Juni ...

  8. RFC函数的初步使用-同步

    1.由于没有外围系统,采用不同SAP不同client之间进行测试. 首先在A-client搭建需要被调用的RFC函数.在A-client里运行SE37创建函数 在属性页签选择“远程启用的模块” 设定i ...

  9. 【机器学习笔记】ID3构建决策树

    好多算法之类的,看理论描述,让人似懂非懂,代码走一走,现象就了然了. 引: from sklearn import tree names = ['size', 'scale', 'fruit', 'b ...

  10. ASP.NET MVC EF 连接数据库(一)-----Database First

    database first (VS2015 ,Sql Server2014) 1,新建MVC项目 实例:   源码代码:http://note.youdao.com/noteshare?id=1fd ...