[BZOJ2124]等差子序列/[CF452F]Permutation
[BZOJ2124]等差子序列/[CF452F]Permutation
题目大意:
一个\(1\sim n\)的排列\(A_{1\sim n}\),询问是否存在\(i,j(i<j)\),使得\(A_i<A_j\)且\(\frac{A_i+A_j}2\)在\(i,j\)之间出现。
BZOJ上的数据范围:\(n\le10000\);
CF上的数据范围:\(n\le3\times10^5\)。
思路:
从左到右枚举每一个数,用两个布尔数组\(b_0,b_1\)分别维护数值为\(i\)的数是否在当前数的左边、右边出现。然后将与当前数差值相等的位置对应起来(如,当前\(A_i=3\)时,将\(b_{0,1}\)与\(b_{1,5}\)对应起来),看一下对应位置有没有都是\(1\)的,如果有,则说明存在。
使用bitset
优化可以做到\(\mathcal O(\frac{n^2}{32})\),但还是过不了。
发现如果只用一个数组\(b\)维护左边出现过的数,那么对于当前位置\(i\),若以\(b_{A_i}\)为中心的极大字符串是不是回文串,说明一个在左边出现,一个在右边出现,那么一定存在解。而确定中心的回文串判定可以用树状数组维护哈希实现。
事件复杂度\(\mathcal O(n\log n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=300001;
const unsigned base=13;
unsigned pwr[N];
int n;
class FenwickTree {
private:
unsigned val[N];
int lowbit(const int &x) const {
return x&-x;
}
unsigned query(const int &p) const {
unsigned ret=0;
for(register int i=p;i;i-=lowbit(i)) {
ret+=val[i]*pwr[p-i];
}
return ret;
}
public:
void modify(const int &p) {
for(register int i=p;i<=n;i+=lowbit(i)) {
val[i]+=pwr[i-p];
}
}
unsigned query(const int &l,const int &r) const {
return query(r)-query(l-1)*pwr[r-l+1];
}
};
FenwickTree t[2];
int main() {
n=getint();
for(register int i=pwr[0]=1;i<=n;i++) {
pwr[i]=pwr[i-1]*base;
}
bool ans=false;
for(register int i=1;i<=n;i++) {
const int x=getint();
const int len=std::min(x-1,n-x);
ans|=t[0].query(x-len,x-1)!=t[1].query(n-x-len+1,n-x);
t[0].modify(x);
t[1].modify(n-x+1);
}
puts(ans?"YES":"NO");
return 0;
}
[BZOJ2124]等差子序列/[CF452F]Permutation的更多相关文章
- bzoj2124: 等差子序列线段树+hash
bzoj2124: 等差子序列线段树+hash 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2124 思路 找大于3的等差数列其实就是找等于 ...
- BZOJ2124 等差子序列(树状数组+哈希)
容易想到一种暴力的做法:枚举中间的位置,设该位置权值为x,如果其两边存在权值关于x对称即合法. 问题是如何快速寻找这个东西是否存在.考虑仅将该位置左边出现的权值标1.那么若在值域上若关于x对称的两权值 ...
- [bzoj2124]等差子序列_线段树_hash
等差子序列 bzoj-2124 题目大意:给定一个1~n的排列,问是否存在3个及以上的位置上的数构成连续的等差子序列. 注释:$1\le n\le 10^4$. 想法:这题就相当于是否存在3个数i,j ...
- [bzoj2124]等差子序列(hash+树状数组)
我又来更博啦 2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 941 Solved: 348[Submit][Statu ...
- bzoj2124 等差子序列(hash+线段树)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 719 Solved: 261[Submit][Status][Discuss] ...
- BZOJ2124:等差子序列(线段树,hash)
Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pLen<=N (Len>=3), 使得A ...
- BZOJ2124: 等差子序列(树状数组&hash -> bitset 求是否存在长度为3的等差数列)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 2354 Solved: 826[Submit][Status][Discuss ...
- BZOJ2124: 等差子序列
题意:给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4<p5<…<pLen<=N(Len>=3),使得 Ap1,Ap ...
- [bzoj2124]等差子序列——线段树+字符串哈希
题目大意 给一个1到N的排列\(A_i\),询问是否存在\(p_i\),\(i>=3\),使得\(A_{p_1}, A_{p_2}, ... ,A_{p_len}\)是一个等差序列. 题解 显然 ...
随机推荐
- expect 交互
expect expect 是一种自动交互语言,能实现在shell脚本中为scp和ssh等自动输入密码自动登录. exp_continue # 多个spawn命令时并行 interact # 执行完成 ...
- 第11月第8天 ffmpeg ffplay
static int ffplay_video_thread(void *arg) { FFPlayer *ffp = arg; VideoState *is = ffp->is; AVFram ...
- [转]OpenMP 入门指南
简介 这门课作为 ECE 中少有的跟计算机科学相关的课,自然是必上不可.不过无论是 OpenMP 还是 CUDA,对于平时极少接触并行编程的我来说,都是十分吃力的,第一次作业的 OpenMP 编程已经 ...
- springMVC版本和jdk版本不匹配造成的问题
一个简单的例子项目,使用springMVC的版本是3.2,jdk的版本是1.7,使用的是注解的处理器适配器和处理器映射器.spring的xml配置文件中单独配置每个handler,可以正常的使用,如果 ...
- usb device address error 110
ubuntu失灵了,怎么都起不来,报一堆错误usb device descriptor read/64, error 110......重启,换kvm的接口,usb键盘鼠标...终于在试了下面这个方法 ...
- win7下PHP+MySQL+CoreSeek中文检索引擎配置
1.Windows下的coreseek安装测试 (64位win7旗舰版) 官方参考:http://www.coreseek.cn/products-install/install_on_windows ...
- 远程不能访问CentOS的tomcat 8080端口
一般认为是CentOS的iptabls防火墙的问题,方法如下: ps -ef | grep tomcat ker 4399 1 6 21:46 pts/1 00:00:01 /usr/java/jdk ...
- 通过anaconda进行python多版本控制
---恢复内容开始--- linux与windows通用. 1. 假设电脑上已经转好anaconda3. (anaconda 默认装好了python3.jupyter.spyter) 2. 现在需求是 ...
- cube-ui
cube-ui 新官网:https://didi.github.io/cube-ui/#/zh-CN
- linux 命令点滴记录(centos)
2016年5月26日:创建root用户 [lx@localhost ~]$ su root Password: [root@localhost lx]# 帐号:root ;密码:输入的Password ...