您的位置:首页 > 其它

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

2018-01-30 16:11 281 查看
题目描述







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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: