【gym102394L】LRU Algorithm(自然溢出哈希)
题意:给定一个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(自然溢出哈希)的更多相关文章
- 自然溢出哈希 hack 方法
今天不知道在什么地方看到这个东西,感觉挺有意思的,故作文以记之( 当 \(base\) 为偶数时,随便造一个长度 \(>64\) 的字符串,只要它们后 \(64\) 位相同那么俩字符串的哈希值就 ...
- [CCPC2019 哈尔滨] L. LRU Algorithm - 哈希
[CCPC2019 哈尔滨] L. LRU Algorithm Description 对一个序列执行 LRU 算法.每次询问给定一个窗口,问它是否出现过. Solution 很显然我们可以先假设窗口 ...
- LRU Algorithm Gym - 102394L (HASH)
LRU Algorithm \[ Time Limit: 1000 ms\quad Memory Limit: 524288 kB \] 题意 给出 \(n\) 个数字和 \(m\) 次查询. 每次询 ...
- 关于如何让写自然溢出hash的无辜孩子见祖宗这件事
关于如何让写自然溢出hash的无辜孩子见祖宗这件事 来源博客 这几天考试连着好几次被卡hash卡到死. 我谔谔,为什么连hash都要卡. 码力弱鸡什么时候才能站起来. 只需要任意两种字符,比如噫呜呜噫 ...
- Operating System Memory Management、Page Fault Exception、Cache Replacement Strategy Learning、LRU Algorithm
目录 . 引言 . 页表 . 结构化内存管理 . 物理内存的管理 . SLAB分配器 . 处理器高速缓存和TLB控制 . 内存管理的概念 . 内存覆盖与内存交换 . 内存连续分配管理方式 . 内存非连 ...
- 转:MD5(Message-Digest Algorithm 一种哈希算法)
什么是MD5算法 MD5讯息摘要演算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码杂凑函数,可以产生出一个128位元(16位元组)的散列值(hash val ...
- [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)
局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...
- [algorithm][security] 模糊哈希(转)
modsecurity中用到: http://ssdeep.sourceforge.net/ 原文:http://www.xuebuyuan.com/1536438.html 最近看一篇paper, ...
- 【BZOJ3529】数表(莫比乌斯反演,BIT,自然溢出)
题意: 思路: #include<cstdio> #include<cstring> #include<string> #include<cmath> ...
随机推荐
- windows VS2013中使用<pthread.h>
1. 下载pthreads-w32-2-9-1-realease.zip 地址:http://www.mirrorservice.org/sites/sourceware.org/pub/pthrea ...
- Linux上面执行 Windows 命令(比如 重启服务)的简单方法
1. 首先 基础是:openssh 还有 expect的包 2. 方法 安装openssh 转帖来自: https://www.jianshu.com/p/6e5bc39d386e 最近项目在搞Jen ...
- css overflow规定元素溢出框将会发生什么
- requests实现文件下载, 期间显示文件信息&下载进度_python3
requests实现文件下载, 期间显示文件信息&下载进度 """使用模块线程方式实现网络资源的下载 # 实现文件下载, 期间显示文件信息&下载进度 # ...
- 085、如何快速部署 Prometheus (2019-05-07 周二)
参考https://www.cnblogs.com/CloudMan6/p/7724576.html 部署环境: 两台 Docker Host 10.12.31.211 10.12.3 ...
- Struts2对于BigDecimal类型的转换问题
Struts2对常用的数据类型如String.Integer.Double等都添加了转换器进行对应的转换操作. BigDecimal其实也算作是一种常用的数据类型,但Struts2没有对该类型设置转换 ...
- springboot项目抓数据后优化配置及四个补充
昨天搞了一个抓取某某平台信息的抓取功能,其中有一个地址url,昨天是写死的,之前也进行配置过,印象有些模糊,今天想配置一下,在properties文件中,由此引发了下面的一系列总结操作: 1.原始模式 ...
- 【leetcode】 463. Island Perimeter
题目: 以二维数组形式表示坐标岛屿,求边长. 例子: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] Answer: 16 Explanation: The ...
- PHP的htmlspecialchars、strip_tags、addslashes
PHP的htmlspecialchars.strip_tags.addslashes是网页程序开发中常见的函数,今天就来详细讲述这些函数的用法: 1.函数strip_tags:去掉 HTML 及 PH ...
- java面试02——基础
1. JDK . JRE 和JVM有什么区别? JDK:Java Development Kit 的简称,Java 开发工具包,提供了 Java 的开发环境和运行环境. JRE:Java Runtim ...