2124: 等差子序列 - BZOJ
Description
给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列。
Input
输入的第一行包含一个整数T,表示组数。下接T组数据,每组第一行一个整数N,每组第二行为一个1到N的排列,数字两两之间用空格隔开。
Output
对于每组数据,如果存在一个等差子序列,则输出一行“Y”,否则输出一行“N”。
Sample Input
2
3
1 3 2
3
3 2 1
Sample Output
N
Y
HINT
对于100%的数据,N<=10000,T<=7
首先觉得题目不错(2013年我竟然在wikioi过了,代码还很短,然后把代码翻出来,发现是暴力n方加优化代码。。。。。。汗,我当时怎么想的)
今天看到题目觉得没什么想法,于是就翻了题解http://seter.is-programmer.com/posts/31902.html
就是维护每一个数字a[i]之前出现a[i]-1,a[i]-2,a[i]-3...的出现情况和a[i]+1,a[i]+2,a[i]+3...的出现情况(用二进制)
如果这两个二进制数的第k位不同,那么就是a[i]-k在a[i]前出现了,a[i]+k在a[i]之后出现或者a[i]+k在a[i]前出现了,a[i]-k在a[i]之后出现,所以我们只要判断两个二进制不同,就可以用hash值来判断了(不放心的话可以做两个hash),用线段树维护hash值就行了
const
maxn=;
h=;
type
node=record
l,r,lc,rc:longint;
h1,h2:int64;
end;
var
f:array[..maxn*]of node;
a:array[..maxn]of longint;
p:array[..maxn]of int64;
t,n,tot:longint; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; function max(x,y:longint):longint;
begin
if x>y then exit(x);
exit(y);
end; procedure add(x,now:longint);
var
mid:longint;
begin
if f[now].l=f[now].r then
begin
f[now].h1:=;
f[now].h2:=;
exit;
end;
mid:=(f[now].l+f[now].r)>>;
if x>mid then add(x,f[now].rc)
else add(x,f[now].lc);
f[now].h1:=(f[f[now].lc].h1*p[f[now].r-mid]+f[f[now].rc].h1)mod h;
f[now].h2:=(f[f[now].rc].h2*p[mid-f[now].l+]+f[f[now].lc].h2)mod h;
end; procedure build(l,r:longint);
var
now,mid:longint;
begin
inc(tot);
now:=tot;
f[now].l:=l;
f[now].r:=r;
f[now].h1:=;
f[now].h2:=;
f[now].lc:=;
f[now].rc:=;
if l=r then exit;
mid:=(l+r)>>;
f[now].lc:=tot+;
build(l,mid);
f[now].rc:=tot+;
build(mid+,r);
end; function hash1(l,r,now:longint):int64;
var
mid:longint;
begin
if (l<=f[now].l) and (r>=f[now].r) then exit(f[now].h1);
mid:=(f[now].l+f[now].r)>>;
if l>mid then exit(hash1(l,r,f[now].rc));
if r<=mid then exit(hash1(l,r,f[now].lc));
exit((hash1(l,r,f[now].lc)*p[min(r,f[now].r)-mid]+hash1(l,r,f[now].rc))mod h);
end; function hash2(l,r,now:longint):int64;
var
mid:longint;
begin
if (l<=f[now].l) and (r>=f[now].r) then exit(f[now].h2);
mid:=(f[now].l+f[now].r)>>;
if l>mid then exit(hash2(l,r,f[now].rc));
if r<=mid then exit(hash2(l,r,f[now].lc));
exit((hash2(l,r,f[now].rc)*p[mid-max(l,f[now].l)+]+hash2(l,r,f[now].lc))mod h);
end; procedure main;
var
i:longint;
begin
read(n);
for i:= to n do
read(a[i]);
tot:=;
build(,n);
for i:= to n- do
begin
add(a[i-],);
if hash1(a[i]-min(a[i]-,n-a[i]),a[i],)=hash2(a[i],a[i]+min(a[i]-,n-a[i]),) then continue;
writeln('Y');
exit;
end;
writeln('N');
end; begin
p[]:=;
for t:= to maxn do
p[t]:=p[t-]* mod h;
read(t);
while t> do
begin
dec(t);
main;
end;
end.
2124: 等差子序列 - BZOJ的更多相关文章
- bzoj 2124 等差子序列 (线段树维护hash)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 1922 Solved: 714[Submit][Status][Discuss ...
- BZOJ 2124: 等差子序列 线段树维护hash
2124: 等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一 ...
- BZOJ 2124: 等差子序列
Sol 线段树+Hash. 首先暴力 等差子序列至少3项就可以了,就枚举中项,枚举公差就可以了,只需要一个数在中项前出现,另一个数在中项前没出现过就可以了.复杂度 \(O(n^2)\) 然后我想了一个 ...
- BZOJ 2124等差子序列 线段树&&hash
[题目描述 Description] 给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4<p5<…<pLen<=N(Len& ...
- bzoj 2124 等差子序列 树状数组维护hash+回文串
等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 1919 Solved: 713[Submit][Status][Discuss] Desc ...
- [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 -> bitset 求是否存在长度为3的等差数列)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 2354 Solved: 826[Submit][Status][Discuss ...
- BZOJ [P2124] 等差子序列
线段树维护哈希值 要求出现长度大于三的等差子序列,我们只要找到长度等于三的就可以了 初看本题没有思路,只能暴力枚举,O(n^4) 后来发现,这个序列是n的一个排列,那么每个数字都只会出现一次 我们可以 ...
随机推荐
- 面向对象的异常处理之深入理解java异常处理机制
什么是异常? 异常是对问题的描述,将问题的对象进行封装: 异常体系的特点:异常体系中的所有类以及建立的对象: 都具有可抛性,也就是说可以被throw和throws关键字所操作,只有异常体系具有该特点: ...
- FreeBSD修改root密码错误passwd: pam_chau(www.111cn.net)thtok(): error in service module from:http://www.111cn.net/sys/freebsd/66713.htm
在FreeBSD中修改帐号密码有时候会出现一些错误,针对passwd: pam_chauthtok(): error in service module这样的错误提示,简单整理了以下解决方案:错误提示 ...
- 新手学习ios开发的辅助工具
完整APP项目源码: Objective-C https://github.com/singro/v2ex Swift https://github.com/YANGReal/JokeClient-S ...
- ios中的事件处理、响应者链条以及第一响应者
在ios中,事件UIEvent类来表示,当一个事件发生时,系统会搜集的相关事件信息,创建一个UIEvent对象,最后将该事件转发给应用程序对象(UIApplication).日常生活中,主要有三种类型 ...
- Java longTime 和C#日期转换(结构+运算符重载)
前几天,因为工作原因,连到了公司的一个java系统.查看数据的时候,突然整个人都不好了,数据库中日期字段时间为毛都是整型?之前从来没有接触过java,所心就趁机了解了一下.原来,在数据库中,保存的是j ...
- js 函数的调用模式
1.函数调用 调用一个函数将暂停当前函数的执行,传递控制权和参数给新函数.除了函数声明时定义的形参,每个函数还接受两个附加的参数:this和arguments(arguments并不是一个真正的数组, ...
- JavaIO和JavaNIO
BIO和NIO BIO在之前的服务器处理模型中,在调用ServerSocket.accept()方法时,会一直阻塞到有客户端连接才会返回,每个客户端连接过来后,服务端都会accept一个新连接,接着启 ...
- AJAX 跨域 :Access-Control-Allow-Origin
在一个项目上想用NodeJS,在前端的JS(http://localhost/xxx)中ajax访问后端RestAPI(http://localhost:3000/….)时(Chrome)报错: XM ...
- Entity Framework 并发处理借鉴
如下博客 http://www.cnblogs.com/Bce-/p/3725868.html
- DevExpress控件水印文字提示
ButtonEdit.Properties.NullValuePrompt = "提示"; ButtonEdit.Properties.NullValuePromptShowFor ...