[BZOJ2761/Luogu4305][JLOI2011]不重复数字 题解

题目链接:

BZOJ2761

Luogu4305

大水题一个。。。

应该有很多种做法,(Juruo)随便说几个自己的解法吧。

Plan A

最暴力的:用一颗平衡树记录前面出现过的数,判断当前数是不是第一次出现。

这个过程可以用(std::set/std::map)实现。

然后我的(set)(Luogu)上蜜汁(TLE)??

(O2)水过

结论:BZOJ比Luogu快

时间复杂度:(O(Tnlog_2n))

#include <set>
#include <cstdio>
#include <cctype>

inline int Getint()
{
	register int x=0,f=1,c;
	while(!isdigit(c=getchar()))if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=x*10+(c^48);
	return x*f;
}

std::set<int> App;

int main()
{
	for(int t=Getint();t--;)
	{
		App.clear();
		int n=Getint();
		for(bool First=true;n--;)
		{
			int x=Getint();
			if(!App.count(x))
			{
				App.insert(x);
				if(First)First=false;
				else putchar(' ');
				printf("%d",x);
			}
		}
		putchar('
');
	}
	return 0;
}

Plan B

在上面的基础修改,把平衡树改成(Hash)表,这样复杂度就变成了优秀的(O(Tn))

当然可能被卡成(O(Tn^2))

Plan C

其实原题数据极水,直接开桶可过

于是这题就变成了愉快的普及难度

Plan D

可以先对序列排序

先比大小,相同的比下标((std::stable\_sort))

然后(Unique)一遍,再按大小排回来。

时间复杂度不变,常数小了很多。

原文地址:https://www.cnblogs.com/LanrTabe/p/10149397.html