题意:给出n个数的序列,并给出x,这n个数的范围为[1,x],f(L,R)表示删除序列中取值为[l,r]的数,问有几对L,R使得操作后的序列为非递减序列

思路:若[l,r]成立,那么[l,r+1],.....,[l,x]都成立,且若[l,r]成立,那么[l+1,r]不成立,不存在[l+1,r-1]成立, 所以可以看出本题区间具有单调性,可以用双指针求解

说明:\(t_i\)表示i值的最右边坐标,\(s_i\)表示i值的最左边坐标

若f(l,r)成立要满足三个条件

1.\(max(t_1,t_2,...,t_l)<min(s_r,s_{r+1},....s_x)\)

2.\(max(t_1,t_2,...,t_{i-1})<s[i](1<=i<=l)\)

3.\(t_{i}<min(s_{i+1},s_{i+2},....s_x)(r<=i<=x)\)

这里可以用类似前缀和的思想把这几个条件预处理处理,然后直接套用双指针即可

Reference:

https://www.cnblogs.com/henry-1202/p/10888691.html (大佬博客)

https://baijiahao.baidu.com/s?id=1615129382322508344&wfr=spider&for=pc (洛谷双指针总结)

https://www.cnblogs.com/xyq0220/p/10875872.html

#include<bits/stdc++.h>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define mkp make_pair
#define all(zzz) (zzz).being(),(zzz).end()
#define pii pair<long long ,int>
typedef long long ll;
typedef long long LL;
using namespace std;
const int maxn=1e6+7;
int s[maxn],t[maxn],ss[maxn],tt[maxn],a[maxn],precan[maxn],lastcan[maxn];
bool check(int l,int r){
return precan[l-1]&&lastcan[r+1]&&ss[r+1]>tt[l-1];
}
int main(){
int n,x;
scanf("%d%d",&n,&x);
memset(s,0x3f3f3f3f,sizeof(s));
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
s[a[i]]=min(s[a[i]],i);
t[a[i]]=max(t[a[i]],i);
} long long ans=0;
for(int i=1;i<=x;i++){
tt[i]=max(tt[i-1],t[i]);
}
ss[x+1]=0x3f3f3f3f;
for(int i=x;i>=1;i--){
ss[i]=min(ss[i+1],s[i]);
}
precan[0]=1;
lastcan[x+1]=1;
for(int i=1;i<=x;i++)precan[i]=precan[i-1]&&(tt[i-1]<s[i]);
for(int i=x;i>=1;i--)lastcan[i]=lastcan[i+1]&&(ss[i+1]>t[i]);
int r=1;
for(int l=1;l<=x;l++){
if(l>r)r++;
while(r<x&&!check(l,r))r++;
if(check(l,r))ans+=x-r+1;
}
cout<<ans<<endl;
return 0;
}

1167E - Range Deleting 双指针的更多相关文章

  1. Codeforces 1167 E Range Deleting 双指针+思维

    题意 给一个数列\(a​\),定义\(f(l,r)​\)为删除\(a​\)中所有满足\(l<=a_i<=r​\)的数后的数列,问有多少对\((l,r)​\),使\(f(l,r)​\)是一个 ...

  2. CF1167E. Range Deleting

    题意 给定长度为\(n\)的数组\(a\),其中任意\(a_i \leq x\) 定义\(f(l,r)\)为删除\(a\)中值域在\([l,r]\)的数后剩余的数组. 统计满足\(1\leq l \l ...

  3. Educational Codeforces Round 65 (Rated for Div. 2) E. Range Deleting(思维+coding)

    传送门 参考资料: [1]:https://blog.csdn.net/weixin_43262291/article/details/90271693 题意: 给你一个包含 n 个数的序列 a,并且 ...

  4. Educational Codeforces Round 65 E,F

    E. Range Deleting 题意:给出一个序列,定义一个操作f(x,y)为删除序列中所有在[x,y]区间内的数.问能使剩下的数单调不减的操作f(x,y)的方案数是多少. 解法:不会做,思维跟不 ...

  5. Educational Codeforces Round 65 选做

    好久没更博客了,随便水一篇 E. Range Deleting 题意 给你一个长度为 \(n\) 的序列 \(a_1,a_2,\dots a_n\) ,定义 \(f(l,r)\) 为删除 \(l\le ...

  6. Codeforces Edu Round 65 A-E

    A. Telephone Number 跟之前有一道必胜策略是一样的,\(n - 10\)位之前的数存在\(8\)即可. #include <iostream> #include < ...

  7. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  8. [ Educational Codeforces Round 65 (Rated for Div. 2)][二分]

    https://codeforc.es/contest/1167/problem/E E. Range Deleting time limit per test 2 seconds memory li ...

  9. codeforces Educational Codeforces Round 65 (补完)

    C News Distribution 并查集水题 D Bicolored RBS 括号匹配问题,如果给出的括号序列nesting depth为n,那么最终可以分成两个nesting depth为n ...

随机推荐

  1. gulp常用插件之gulp-size使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-size这是一款显示项目的大小插件. 更多使用文档请点击访问gulp-size工具官网. 安装 一键安装不多解释 npm install ...

  2. laravel 解决 sql mode only_full_group_by

    this is incompatible with sql_mode=only_full_group_by 先贴报错是这样的哦,sql 中使用到了 group by 然后这是mysql-5.7以上版本 ...

  3. 服务器的公网ip 和内网ip

    原文地址:https://zhidao.baidu.com/question/814783729071869532.html 服务器公网ip 可以用于域名解析ip,服务器远程登录ip,是最主要的服务器 ...

  4. [CF1303A] Erasing Zeroes

    Solution 找到边界然后循环扫一遍数个数即可 #include <bits/stdc++.h> using namespace std; int n; const int N = 1 ...

  5. linux 搭建python虚拟环境

    requirements.txt 包含paramiko,pysfp.setuptools,适用python版本3.6.6+ 前提编译安装python wget wget https://www.pyt ...

  6. ES6实现图片切换特效

    效果图 demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  7. 850. Dijkstra求最短路 II(堆优化模板)

    给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为非负值. 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1. 输入格式 第一行包含整数n和m. 接下来m行每行 ...

  8. npm命令笔记-----转自网络,仅供自己查看使用

    npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装 的包. npm常用指令 1.npm ...

  9. vue配置开发,测试,生产环境api

    npm run build 调用开发环境接口,打包开发环境npm run build:test 调用测试环境接口,打包测试环境npm run build:prod 调用生产环境接口,打包生产环境 vu ...

  10. VTK坐标系统及视图分割

    计算机图像学里广泛应用的坐标系统有四种,分别是:模型坐标系统(model),世界坐标系统(world),视图坐标系统(view)和显示坐标系统(display). 模型坐标系统就是定义模型时所用的坐标 ...