LRU Algorithm Gym - 102394L (HASH)

LRU Algorithm

\[ Time Limit: 1000 ms\quad Memory Limit: 524288 kB \]

The meaning of problems

Given \ (n-\) numbers and \ (m \) queries.
Each interrogation, given \ (L \) represents \ (the LRU \) size of the buffer, and then given \ (L \) digits of Q \ (n-\) digits do size \ (L \) a \ (the LRU \) process, will not have a time, a sequence of numbers and a buffer given \ (L \) numbers consistent.

Thinking

For the first \ (the LRU \) algorithm, when the buffer size \ (L \) when, in a location \ (POS \) end of the buffer element, is essentially the \ (POS \) onward \ (L \) a different number first appears. So every query we just want to find a way to end each position, forward \ (L \) different numbers of what is, and then determine whether the query sequence and the same, you can know \ (Yes \) or is \ (No \) a.

First discovered \ (n-5000 = \) , it can be \ (n ^ 2 \) violent pretreated out \ (has [I] [J] \) , represents the \ (I \) start forward \ (J \ ) different numbers constituted \ (\) the hash value, then as long as the determination is given \ (L \) digits \ the hash \) ( value meets the condition on it. Note that a judgment dissatisfied \ (L \) case elements, it is also required to find \ (pos \) exists only figures given in the previous location.

/*************************************************************** 
    > 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;
}

Guess you like

Origin www.cnblogs.com/Jiaaaaaaaqi/p/11971234.html