1167E - Range Deleting 双指针
题意:给出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 双指针的更多相关文章
- Codeforces 1167 E Range Deleting 双指针+思维
题意 给一个数列\(a\),定义\(f(l,r)\)为删除\(a\)中所有满足\(l<=a_i<=r\)的数后的数列,问有多少对\((l,r)\),使\(f(l,r)\)是一个 ...
- CF1167E. Range Deleting
题意 给定长度为\(n\)的数组\(a\),其中任意\(a_i \leq x\) 定义\(f(l,r)\)为删除\(a\)中值域在\([l,r]\)的数后剩余的数组. 统计满足\(1\leq l \l ...
- Educational Codeforces Round 65 (Rated for Div. 2) E. Range Deleting(思维+coding)
传送门 参考资料: [1]:https://blog.csdn.net/weixin_43262291/article/details/90271693 题意: 给你一个包含 n 个数的序列 a,并且 ...
- Educational Codeforces Round 65 E,F
E. Range Deleting 题意:给出一个序列,定义一个操作f(x,y)为删除序列中所有在[x,y]区间内的数.问能使剩下的数单调不减的操作f(x,y)的方案数是多少. 解法:不会做,思维跟不 ...
- Educational Codeforces Round 65 选做
好久没更博客了,随便水一篇 E. Range Deleting 题意 给你一个长度为 \(n\) 的序列 \(a_1,a_2,\dots a_n\) ,定义 \(f(l,r)\) 为删除 \(l\le ...
- Codeforces Edu Round 65 A-E
A. Telephone Number 跟之前有一道必胜策略是一样的,\(n - 10\)位之前的数存在\(8\)即可. #include <iostream> #include < ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- [ 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 ...
- codeforces Educational Codeforces Round 65 (补完)
C News Distribution 并查集水题 D Bicolored RBS 括号匹配问题,如果给出的括号序列nesting depth为n,那么最终可以分成两个nesting depth为n ...
随机推荐
- sqli-labs less-15 --> less-16
Less-15(布尔/时间盲注) 1.判断是否存在注入点 无论是输入admin/admin’/admin” 都显示回显正常,于是执行uname=admin' and sleep(5)#&pas ...
- VS打包程序步骤
1.下载打包的程序 2.在你的程序里面安装打包的项目 3.添加项目输出 4.为项目添加必要的文件 双击前面建立好的主输出 一般文件为一些配置文件(如使用Nlog写日志,需要添加Nlog的配置文件)和图 ...
- kubernetes nodePort、targetPort、port、containerPort图解
1. nodePort 外部机器可访问的端口. 比如一个Web应用需要被其他用户访问,那么需要配置type=NodePort,而且配置nodePort=,那么其他机器就可以通过浏览器访问scheme: ...
- 野路子码农系列(8)我终于大致搞懂了GBDT
由于下下周要在组里介绍一个算法,最近开始提前准备,当初非常自信地写下自己最喜欢的GBDT,但随着逐步深入,发现其实自己对这个算法的细节并不是非常了解,了解的只是一些面试题的答案而已……(既然没有深入了 ...
- File FileStream StreamReader和StreamWriter
File 静态类 ReadAllBytes 和 WriteAllBytes ,用于一次性全部读取和写入小文件的字节码, ReadLine ReadkAll 用于一 ...
- 全网最详细——Testlink在windows环境下搭建;提供环境下载
参考这篇文章,写的真不错https://www.jianshu.com/p/6c4321de26ea 工具下载 链接:https://pan.baidu.com/s/1_yzCIvsExbfzcRdl ...
- pandas 读取excel时,遇到数字变为科学计数法了, 怎么破?? 别慌 这样来处理
# 指定字段以string 方式读取 df = pd.read_excel("./test.xlsx", converters={"id": str})
- [HAOI2011] Problem b - 莫比乌斯反演
复习一下莫比乌斯反演 首先很显然用一下容斥把它转化成求 \(ans=\sum_{i=1}^a \sum_{j=1}^b [{gcd(i,j)=d}]\) 我们可以定义 f(d) 和 F(d) 如下: ...
- Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type
问题描述 将项目挂载到 Myeclipse 的 tomcat 上,启动 tomcat ,报错“Initialization of bean failed; nested exception is ja ...
- 二分-G - 4 Values whose Sum is 0
G - 4 Values whose Sum is 0 The SUM problem can be formulated as follows: given four lists A, B, C, ...