题意:给定一个n个数的数字序列,第i个数为a[i],每次操作会将a[i]插入或移到最前端:

1.若a[i]已经在序列中出现过,则将其移到最前端,并删除原出现位置

2.若a[i]未出现过,则直接将其插入到最前端

有q个询问,每个询问给出一个长度为m的序列,问是否在某个时刻询问序列与操作的序列相同,忽略后缀的0

n<=5e3,q<=2e3,sigma m<=2e6

思路:做法一:

实现参考了claris的代码,使用自然溢出哈希,下标标记的方法避免了重新排序或者map之类的带log,现场应该必挂

cf打多了确实喜欢为了方便加个map多个log之类的,还是要做BZOJ的时限紧张题

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef pair<ll,ll>P;
#define N 200010
#define M 1000000
#define INF 1e9
#define fi first
#define se second
#define MP make_pair
#define pb push_back
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1
#define fors(i) for(auto i:e[x]) if(i!=p) const int MOD=1e8+,inv2=(MOD+)/;
int p=1e4+;
double eps=1e-;
int dx[]={-,,,};
int dy[]={,,-,}; int a[N],b[N],ans[N],len[N],n;
ull c[N],h[N]; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} ll readll()
{
ll v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} void calc(int n)
{
rep(i,,n) h[i]=h[i-]*p+b[i];
} void add(int x)
{
int k=;
rep(i,,n)
if(b[i]==x){k=i; break;}
if(k)
{
per(i,k,) b[i]=b[i-];
b[]=x;
}
else
{
per(i,n+,) b[i]=b[i-];
b[]=x;
}
} void solve()
{
n=read();
int q=read();
rep(i,,n) a[i]=read();
rep(i,,q)
{
int m=read();
len[i]=m;
rep(j,,m) b[j]=read();
calc(m);
c[i]=h[m];
ans[i]=;
}
rep(i,,n) b[i]=;
rep(i,,n)
{
if(i) add(a[i]);
calc(n);
rep(j,,q)
if(h[len[j]]==c[j]) ans[j]=;
}
rep(i,,q)
if(ans[i]) printf("Yes\n");
else printf("No\n");
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int cas=read();
while(cas--) solve();
return ;
}

【gym102394L】LRU Algorithm(自然溢出哈希)的更多相关文章

  1. 自然溢出哈希 hack 方法

    今天不知道在什么地方看到这个东西,感觉挺有意思的,故作文以记之( 当 \(base\) 为偶数时,随便造一个长度 \(>64\) 的字符串,只要它们后 \(64\) 位相同那么俩字符串的哈希值就 ...

  2. [CCPC2019 哈尔滨] L. LRU Algorithm - 哈希

    [CCPC2019 哈尔滨] L. LRU Algorithm Description 对一个序列执行 LRU 算法.每次询问给定一个窗口,问它是否出现过. Solution 很显然我们可以先假设窗口 ...

  3. LRU Algorithm Gym - 102394L (HASH)

    LRU Algorithm \[ Time Limit: 1000 ms\quad Memory Limit: 524288 kB \] 题意 给出 \(n\) 个数字和 \(m\) 次查询. 每次询 ...

  4. 关于如何让写自然溢出hash的无辜孩子见祖宗这件事

    关于如何让写自然溢出hash的无辜孩子见祖宗这件事 来源博客 这几天考试连着好几次被卡hash卡到死. 我谔谔,为什么连hash都要卡. 码力弱鸡什么时候才能站起来. 只需要任意两种字符,比如噫呜呜噫 ...

  5. Operating System Memory Management、Page Fault Exception、Cache Replacement Strategy Learning、LRU Algorithm

    目录 . 引言 . 页表 . 结构化内存管理 . 物理内存的管理 . SLAB分配器 . 处理器高速缓存和TLB控制 . 内存管理的概念 . 内存覆盖与内存交换 . 内存连续分配管理方式 . 内存非连 ...

  6. 转:MD5(Message-Digest Algorithm 一种哈希算法)

    什么是MD5算法 MD5讯息摘要演算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码杂凑函数,可以产生出一个128位元(16位元组)的散列值(hash val ...

  7. [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)

    局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...

  8. [algorithm][security] 模糊哈希(转)

    modsecurity中用到:  http://ssdeep.sourceforge.net/ 原文:http://www.xuebuyuan.com/1536438.html 最近看一篇paper, ...

  9. 【BZOJ3529】数表(莫比乌斯反演,BIT,自然溢出)

    题意: 思路: #include<cstdio> #include<cstring> #include<string> #include<cmath> ...

随机推荐

  1. hdfs的balancer

    参考: https://blog.csdn.net/mnasd/article/details/80369603 在CDH中选一个资源多的节点,安装 HDFS->添加角色到实例 启动后状态是灰的 ...

  2. c语言程序命名规范:函数、变量、数组、文件名

    函数: //send or recv data task void send_recv_data(void *pvParameters); //get socket error code. retur ...

  3. 关于Mybatis的Batch模式性能测试及结论(转)

    近日在公司项目中,使用到spring+mybatis的架构,特对mybatis的batch模式做了相关研究,得出以下结论: 1.Mybatis内置的ExecutorType有3种,默认的是simple ...

  4. P1142轰炸

    这是uva上的一道模拟题. 首先给出n(n<=700)个点的坐标(坐标在1*10^9)之内,询问走直线可以经过的点数.一开始我想到了一个类似于桶排序的方法来存坐标,但是要注意数组大小啊!第二次想 ...

  5. <<C++ Primer>> 第四章 表达式

    术语表 第 4 章 表达式 算术转换(arithmetic conversion): 从一种算术类型转换成另一种算术类型.在二元运算符的上下文中,为了保留精度,算术转换通常把较小的类型转换成较大的类型 ...

  6. python正则表达式re 中m.group和m.groups的解释

    转载:http://www.cnblogs.com/kaituorensheng/archive/2012/08/20/2648209.html 先看代码instance: >>> ...

  7. 别人的Linux私房菜(24-25)X Window设置介绍、Linux内核编译与管理

    X Window主要组件为:X Server .X Client . Window Manager . Display Manager. X Server管理硬件,X Client则为应用程序,将所需 ...

  8. 前端页面适配的rem换算 为什么要使用rem

    之前有些适配做法,是通过js动态计算viewport的缩放值(initial-scale). 例如以屏幕320像素为基准,设置1,那屏幕375像素就是375/320=1.18以此类推. 但直接这样强制 ...

  9. java多图片上传

    2017-09-16 <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2 ...

  10. VMware三种连接方式bridge, nat, host-only

    大家在安装完虚拟机后,默认安装了两个虚拟网卡,VMnet1和 VMnet8,其他的未安装(当然也可以手动安装其他的).其中VMnet1是host网卡,用于host方式连接网络的.VMnet8是NAT网 ...