[模拟][哈希]刷题计划

题目描述
这里写图片描述
这里写图片描述
这里写图片描述
Sample Input
10000 12
2 1
3
2 9999
3
1 1
3
2 1
3
2 10000
3
2 9999
3
Sample Output
1
9999 1
9999
9999
10000 9999
9999 10000

分析
其实很简单,只是要把AC的处理掉,陈旧记录处理掉而已
然后其实O(n)朴素查找明明可以,我却作死用哈希没错就好

#include <iostream>
#include <cstdio>
#define q 100037
using namespace std;
int n,m;
int hash[q],wrong[q];
int a[101];
bool w[101];
int locate(int x)
{
    int i=x%q;
    while (hash[i%q]!=0&&hash[i%q]!=x)
    i++;
    return i%q;
}
void insert(int x)
{
    int i=locate(x);
    hash[i]=x;
}
bool member(int x)
{
    int i=locate(x);
    return hash[i]==x;
}
int loc(int x)
{
    int i=x%q;
    while (wrong[i%q]!=0&&wrong[i%q]!=x)
    i++;
    return i%q;
}
void ins(int x)
{
    int i=locate(x);
    wrong[i]=x;
}
void del(int x)
{
    int i=loc(x);
    wrong[i]=0;
}
bool mem(int x)
{
    int i=locate(x);
    return wrong[i]==x;
}
void init()
{
    scanf("%d%d",&n,&m);
}
void doit()
{
    int i,j,p,b,t=0;
    for (i=1;i<=m;i++)
    {
        scanf("%d",&p);
        if (p==1)
        {
            scanf("%d",&b);
            insert(b);
            for (j=1;j<=t;j++)
            if (a[j]==b)
            w[j]=1;
            if (mem(b)) del(b);
        }
        if (p==2)
        {
            scanf("%d",&b);
            if (member(b)) continue;
            if (mem(b))
            for (j=1;j<=t;j++)
            if (a[j]==b)
            w[j]=1;
            t++;
            a[t]=b;
            ins(a[t]);
        }
        if (p==3)
        {
            int k=0;
            for (j=t;j>=1;j--)
            if (!w[j])
            {
                printf("%d ",a[j]);
                k++;
                if (k==20) break;
            }
            printf("\n");
        }
    }
}
int main()
{
    freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);
    init();
    doit();
    fclose(stdin);
    fclose(stdout);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值