bzoj 2761

神题...

其实这题巨水,用各种诡异的方法都能A,包括STL等等

我之所以写题解,是因为我发现了一个bug:bz和luogu时限有问题!

这题我用了两种做法:

①:直接使用STL-map(不能直接用数组,值太大了)记录一个数是否出现过即可,时间复杂度O(nlog2n有常数)

#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
map <int,int> M[55];
int T,n;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            if(M[T][x])
            {
                continue;
            }
            M[T][x]=1;
            printf("%d ",x);
        }
        printf("\n");
    }
    return 0;
}

bzoj AC,luogu TLE3个点

难道是卡常???

换!

②:挂链

利用类似挂链hash的方法,先对整数值取模再挂链查找即可

时间复杂度O(n)(常数不小)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define seed 13151
using namespace std;
map <int,int> M[55];
struct Edge
{
	int next;
	int to;
}edge[50005];
int head[14000];
int cnt=1;
void init()
{
	memset(head,-1,sizeof(head));
	memset(edge,0,sizeof(edge));
	cnt=1;
}
void add(int l,int r)
{
	edge[cnt].next=head[l];
	edge[cnt].to=r;
	head[l]=cnt++;
}
bool check(int v1,int v2)
{
	for(int i=head[v1];i!=-1;i=edge[i].next)
	{
		int to=edge[i].to;
		if(to==v2)
		{
			return 0;
		}
	}
	return 1;
}
int T,n;
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		init();
		for(int i=1;i<=n;i++)
		{
			int x;
			scanf("%d",&x);
			if(!check(x%seed,x))
			{
				continue;
			}
			add(x%seed,x);
			printf("%d ",x);
		}
		printf("\n");
	}
	return 0;
}

luoguAC,bzoj TLE...

其实还有

③:离散化之后直接数组hash

时间复杂度O(nlog2n),瓶颈在于排序,但常数较小

没做尝试... 

 

posted @ 2018-09-29 16:19  lleozhang  Views(80)  Comments(0Edit  收藏  举报
levels of contents