LRU Algorithm Gym - 102394L (HASH)
LRU Algorithm
\]
题意
给出 \(n\) 个数字和 \(m\) 次查询。
每次询问中,给出 \(L\) 表示 \(LRU\) 中缓存器的大小,再给出 \(L\) 个数字,问对 \(n\) 个数字做大小为 \(L\) 的 \(LRU\) 过程中,会不会有某一时刻,缓存器中的数字序列和给出的 \(L\) 个数字一致。
思路
首先对于 \(LRU\) 算法,当缓存器大小为 \(L\)时,以某个位置 \(pos\) 结束的缓存器中的元素,本质上就是从 \(pos\) 往前的 \(L\) 个第一次出现的不同的数字。所以每次查询我们只要想办法找出以每个位置结束,往前 \(L\) 个不同的数字是什么,然后判断和所查询的序列是否一致,就可以知道是 \(Yes\) 或者是 \(No\) 了。
首先发现 \(n=5000\),那么可以 \(n^2\) 暴力预处理出 \(has[i][j]\),表示从 \(i\) 开始,往前 \(j\) 个不同的数字所构成的 \(hash\) 值,然后只要判断给出的 \(L\) 个数字的 \(hash\) 值是否满足条件就可以了。注意判断一下不满 \(L\) 个元素的情况,那就还要求找到的 \(pos\) 位置以前只存在给出的数字。
/***************************************************************
> File Name : L.cpp
> Author : Jiaaaaaaaqi
> Created Time : Tue 12 Nov 2019 04:58:09 PM CST
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 5e3 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
int aa[maxn];
int a[maxn], b[maxn];
int vis[maxn], pre[maxn];
ull has[maxn][maxn], seed = 233;
int main() {
// freopen("in", "r", stdin);
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++) scanf("%d", &aa[i]);
int len1 = 0, len2 = 0;
for(int i=1; i<=n; i++) {
if(n==0 || aa[i]!=a[len1]) a[++len1] = aa[i];
}
for(int i=1; i<=n; i++) vis[i] = pre[i] = 0;
for(int i=1; i<=n; i++) {
pre[i] = pre[i-1];
if(vis[a[i]] == 0) pre[i]++;
vis[a[i]]++;
}
for(int i=n; i>=1; i--) {
for(int j=1; j<=n; j++) vis[j] = 0;
int cnt = 0;has[i][0] = 0;
for(int j=i; j>=1; j--) {
if(vis[a[j]] == 0) {
cnt++;
has[i][cnt] = has[i][cnt-1] * seed + a[j];
}
vis[a[j]]++;
}
}
while(m--) {
scanf("%d", &len2);
for(int i=1; i<=len2; i++) scanf("%d", &b[i]);
bool flag = 0;
while(len2 && b[len2] == 0) len2--, flag = 1;
if(len2 == 0) {
printf("Yes\n");
continue;
}
ull ans = 0;for(int i=1; i<=len2; i++) ans = ans*seed+b[i];
int pos = 0;
for(int i=1; i<=len1; i++) {
if(has[i][len2] == ans) {
pos = i;
break;
}
}
if(pos && (!flag || (flag && pre[pos]==len2))) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
LRU Algorithm Gym - 102394L (HASH)的更多相关文章
- 哈希(Hash)与加密(Encrypt)相关内容
1.哈希(Hash)与加密(Encrypt)的区别 哈希(Hash)是将目标文本转换成具有相同长度的.不可逆的杂凑字符串(或叫做消息摘要),而加密(Encrypt)是将目标文本转换成具有不同长度的.可 ...
- 数据结构之哈希(hash)表
最近看PHP数组底层结构,用到了哈希表,所以还是老老实实回去看结构,在这里去总结一下. 1.哈希表的定义 这里先说一下哈希(hash)表的定义:哈希表是一种根据关键码去寻找值的数据映射结构,该结构通过 ...
- redis学习-散列表常用命令(hash)
redis学习-散列表常用命令(hash) hset,hmset:给指定散列表插入一个或者多个键值对 hget,hmget:获取指定散列表一个或者多个键值对的值 hgetall:获取所欲哦键值以及 ...
- 【Redis】命令学习笔记——哈希(hash)(15个超全字典版)
本篇基于redis 4.0.11版本,学习哈希(hash)相关命令. hash 是一个string类型的field和value的映射表,特别适合用于存储对象. 序号 命令 描述 实例 返回 HSET ...
- 《数据结构与算法分析——C语言描述》ADT实现(NO.05) : 散列(Hash)
散列(Hash)是一种以常数复杂度实现查找功能的数据结构.它将一个关键词Key,通过某种映射(哈希函数)转化成索引值直接定位到相应位置. 实现散列有两个关键,一是哈希函数的选择,二是冲突的处理. 对于 ...
- HDU 1880 魔咒词典 (Hash)
魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 哈希(hash)理解
转载自https://www.cnblogs.com/mingaixin/p/4318837.html 一.什么是哈希?(一种更复杂的映射) Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就 ...
- ruby 对象转换哈希(Hash)
通过 ActiveRecord 从数据库的某张数据表(table)中获取的对象如何转换成为 Hash orders_table 是一张订单信息表,对应的 model 为 Orders @order = ...
- 哈希表(hash)详解
哈希表结构讲解: 哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度. ...
随机推荐
- 判断一个类型是否为可空类型 System Nullable
bool IsNullableType(Type theType) { return (theType.IsGenericType && theType. G ...
- 使用numpy处理数组
def fun_ndarray(): a = [[1,2,7], [-6,-2,-3], [-4,-8,-55] ] b = np.array(a) b = np.abs(b)#取数组的绝对值 pri ...
- linux vsftp查看ftp账号信息的方法
linux vsftp查看ftp账号信息的方法 查看注册的FTP账号 在/etc/vsftpd/chroot_list 密码看不到 只能重置密码 passwd username
- Jenkins打包编码GBK的不可映射字符
1.错误信息如下: 2.在Maven的POM中加入如下代码,然后重新打包即可. <properties> <!-- 文件拷贝时的编码 --> <project.bui ...
- UI事件定位--HitTest
In computer graphics programming, hit-testing (hit detection, picking, or pick correlation) is the p ...
- 【spring boot】spring boot 基于redis pipeline 管道,批量操作redis命令
spring boot 2.x 使用RedisTemplate 操作 =================================== 1.pom.xml <!--spring2.0集成r ...
- 干货满满!如何优雅简洁地实现时钟翻牌器(支持JS/Vue/React)
双十一剁手节过去了,大家应该在很多网页中看到了数字翻牌的效果吧,比如倒计时. 数字增长等.相信很多人都已经自己独立实现过了,我也在网上看了一些demo,发现HTML结构大多比较复杂,用了4个并列的标签 ...
- Consider the following: If you want an embedded database (H2, HSQL or Der...
这个坑把java进程干掉就可以了,因为占用了 Description: Failed to configure a DataSource: 'url' attribute is not specifi ...
- SpringBoot整合PageHelper做多条件分页查询
https://yq.aliyun.com/articles/619586 本篇博客讲述如何在SpringBoot中整合PageHelper,如何实现带多个条件,以及PageInfo中的属性的中文解释 ...
- json串处理2
请求百度地址坐标:http://api.map.baidu.com/location/ip?ak=y0Yb5ZgGK9blTDbR7Dwh9jGtn6X1YE48&coor=bd09ll&am ...