【题解】P4305 [JLOI2011]不重复数字

题目传送门

首先这题加强过,O(Tnlogn)O(Tnlogn) 的 map 肯定是过不去的。但是不知道为什么 unordered_map 应该是 O(Tn)O(Tn) 的也过不去。

然后这是个 hash 表板子,随便处理一下避免出现负数下标就行了。

但是好像这题单模数过不去……需要使用双 hash?然后选取的模数还需要非常精巧(19260817 不可用),不然会 WA 后四个点。

注意双 hash 应该使用 || 而不是 &&,因为双 hash 是要避免两个数冲突结果造成少输出一个数,因此只要其中一个 hash 值没冲突即可。

时间复杂度 O(Tn)O(Tn)。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h>
#define ll long long
#define back return
#define ri register int
using namespace std;
ll read()
{
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
back x*f;
}
const int p=63068673,p1=19491801;
ll t,n;
int a[50005];
bool hash1[63068674],hash2[20000000];
int main()
{
t=read();
while(t--)
{
n=read();
for(ri i=1;i<=n;i++)
a[i]=read(),hash1[(a[i]%p+p)%p]=1,hash2[(a[i]%p1+p1)%p1]=1;
for(ri i=1;i<=n;i++)
{
int lsbl1=(a[i]%p+p)%p,lsbl2=(a[i]%p1+p1)%p1;
if(hash1[lsbl1]||hash2[lsbl2])
cout<<a[i]<<" ",hash1[lsbl1]=hash2[lsbl2]=0;
}
cout<<"\n";
}
back 0;
}