牛客第三场 J LRU management

起初看到这道题的时候,草草就放过去了,开了另一道题,结果开题不顺利,总是感觉差一点就可以做出来,以至于一直到最后都没能看这道题qaq

题意:类似于操作系统上讲的LRU算法,有两个操作,0操作代表访问其中的块,如果命中,将该块去除放到数组的末尾,未命中则在数组末尾加入当前块,1操作是询问数组中是否存在一个块,存在输出该块的数据,否则输出无效。

分析:考虑到只是对于数组的最后一个元素进行操作,所以可以使用链表进行模拟相关操作,主要的坑点就是时间卡的很紧所以需要加入快读,使用简单的hash将字符串映射成一个值,从而可以得到相应的结果。

 

#include<bits/stdc++.h>
#define LL long long
#define null 0
using namespace std;

namespace io {
char buf[1<<21], *p1 = buf, *p2 = buf;
inline char gc() {
    if(p1 != p2) return *p1++;
    p1 = buf;
    p2 = p1 + fread(buf, 1, 1 << 21, stdin);
    return p1 == p2 ? EOF : *p1++;
}

#define G getchar

template<class I>
inline void read(I &x) {
    x = 0; I f = 1; char c = G();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = G(); }
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = G(); }
    x *= f;
}

template<class I>
inline void write(I x) {
    if(x == 0) {putchar('0'); return;}
    I tmp = x > 0 ? x : -x;
    if(x < 0) putchar('-');
    int cnt = 0;
    while(tmp > 0) {
        buf[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while(cnt > 0) putchar(buf[--cnt]);
}

#define in(x) read(x)
#define outn(x) write(x), putchar('\n')
#define out(x) write(x), putchar(' ')

} using namespace io;

#define ll long long
const int N = 100010;

const int maxn=5e5+10;
unordered_map<LL,int >mp;
int x,cnt,xx,op,t,q,cap;
char s[20];
LL temp;
struct node
{
    node *pre,*next;
    LL a;
    int data;
    node()
    {
        pre=null;
        next=null;
        data=0;
    }
};
node *head,*tail,*p[maxn];

void init()
{
    head->next=tail;
    tail->pre=head;
}
void insert()
{
    cnt++;
    p[cnt]->pre=tail->pre;
    tail->pre->next=p[cnt];
    p[cnt]->next=tail;
    tail->pre=p[cnt];
    p[cnt]->data=xx;
    mp[temp]=cnt;
    p[cnt]->a=temp;
}
void del(node *x)
{
    x->pre->next=x->next;
    x->next->pre=x->pre;
    mp[x->a]=0;
}
int main()
{
    int t;
    in(t);
    head=new node();
    tail=new node();
    for(int i=1;i<=maxn;i++)
    {
        p[i]=new node();
    }
    while(t--)
    {
        mp.clear();
        x=0;cnt=0;
        init();
        in(q);
        in(cap);
        while(q--)
        {
            in(op);
            scanf(" %s",s);
            int len=strlen(s);
            temp=0;
            for(int i=0;i<len;i++)
            {
                temp=temp*10+s[i]-'0';
                temp=temp*100+len;
            }
            in(xx);
            if(op==0)
            {
                if(mp[temp]==0)
                {
                    if(x>=cap)
                    {
                        del(head->next);
                        insert();
                        outn(xx);
                    }
                    else
                    {
                        x++;
                        insert();
                        outn(xx);
                    }
                }
                else
                {
                    outn(p[mp[temp]]->data);
                    //printf("%d\n",p[mp[temp]]->data);
                    xx=p[mp[temp]]->data;
                    del(p[mp[temp]]);
                    insert();
                }
            }
            else if(op==1)
            {
                if(mp[temp]==0)
                {
                    printf("Invalid\n");
                }
                else
                {
                    if(xx==0)
                    {
                        outn(p[mp[temp]]->data);
                        //printf("%d\n",p[mp[temp]]->data);
                    }
                    else if(xx==1)
                    {
                        if(p[mp[temp]]->next==tail)
                        {
                            printf("Invalid\n");
                        }
                        else
                        {
                            outn(p[mp[temp]]->next->data);
                            //printf("%d\n",p[mp[temp]]->next->data);
                        }
                    }
                    else if(xx==-1)
                    {
                        if(p[mp[temp]]->pre==head)
                        {
                            //outn("Invalid");
                            printf("Invalid\n");
                        }
                        else
                        {
                            outn(p[mp[temp]]->pre->data);
                           // printf("%d\n",p[mp[temp]]->pre->data);
                        }
                    }
                }
            }

        }

    }
}
View Code

 

posted @ 2019-07-28 21:44  nlc_x  阅读(219)  评论(0编辑  收藏  举报