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的一个排列,那么每个数字都只会出现一次 我们可以 ...
随机推荐
- AsyncQueryHandler 和 CursorAdapter的使用
AsyncQueryHandler A helper class to help make handling asynchronous ContentResolver queries easier. ...
- MyContentProvider
package com.example.shad_fnst.mycontentprovider; import android.app.Activity; import android.content ...
- js标签放在html的什么位置比较好
推荐的是js的script标签放在body的末尾,</body>标签之前,包含在body内! <body> <!--其它Html标签--> <script&g ...
- Excel VBA 快捷键 代码
一. 在ThisWorkbook中 Private Sub Workbook_Open() '%对应alt键 宏不能加() Application.OnKey "%q", &quo ...
- 关于lua垃圾回收是否会执行__gc函数呢?
直接上代码 -- test.lua do local x = setmetatable({},{ __gc = function() print("works") end }) e ...
- (转)手把手教你如何架设VPN
简介 让远程用户连接Exchange Server的传统解决方案是使用Outlook Web Access.然而,为何不使用虚拟专用网(Virtual Private Network,VPN)让你的远 ...
- 用Windows API函数(CreateFile/ReadFile/WriteFile/CloseHandle)完成文件拷贝程序(初级版)
文件拷贝程序 程序类型:Console 参数:源文件名 目的文件名 要求:1.只能使用Windows API函数(CreateFile/ReadFile/WriteFile/CloseHandle ...
- HTML5-Geolocation&地图.html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- linux下配置tomcat7 + solr4.9(续)--- 多核索引的配置
在上一篇文章中(详见http://www.cnblogs.com/bxljoy/p/3850263.html),我们已经介绍了tomcat+solr的索引服务器的配置,但是文中创建的服务器只能支持存储 ...
- 自动设置iframe大小的jQuery代码
自动设置iframe的宽度,如何用jquery来实现呢? 代码: <iframe src="main_folder.aspx" class="global_main ...