2018牛客多校第四场J题 Hash Function(思维+并查集)

题目描述 

Chiaki has just learned hash in today's lesson. A hash function is any function that can be used to map data of arbitrary size to data of fixed size. As a beginner, Chiaki simply chooses a hash table of size n with hash function .
Unfortunately, the hash function may map two distinct values to the same hash value. For example, when n = 9 we have h(7) = h(16) = 7. It will cause a failure in the procession of insertion. In this case, Chiaki will check whether the next position  is available or not. This task will not be finished until an available position is found. If we insert {7, 8, 16} into a hash table of size 9, we will finally get {16, -1, -1, -1, -1, -1, -1, 7, 8}. Available positions are marked as -1.
After done all the exercises, Chiaki became curious to the inverse problem. Can we rebuild the insertion sequence from a hash table? If there are multiple available insertion sequences, Chiaki would like to find the smallest one under lexicographical order.
Sequence a1, a2, ..., an is lexicographically smaller than sequence b1, b2, ..., bn if and only if there exists i (1 ≤ i ≤ n) satisfy that ai < bi and aj = bj for all 1 ≤ j < i.

输入描述:

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line of each case contains a positive integer n (1 ≤ n ≤ 2 x 105) -- the length of the hash table. 
The second line contains exactly n integers a1,a2,...,an (-1 ≤ ai ≤ 109).
It is guaranteed that the sum of all n does not exceed 2 x 106.

输出描述:

For each case, please output smallest available insertion sequence in a single line. Print an empty line when the available insertion sequence is empty. If there's no such available insertion sequence, just output -1 in a single line.

示例1

输入

3
9
16 -1 -1 -1 -1 -1 -1 7 8
4
8 5 2 3
10
8 10 -1 -1 34 75 86 55 88 18

输出

7 8 16
2 3 5 8
34 75 86 55 88 18 8 10

题意:给出按照哈希规则哈出来的序列,给出一个字典序最小的输入顺序.

思路:我们用优先队列使字典序最小,对于没冲突的元素,随时可以放,所以我们一开始就可以把他压入优先队列.

对于有冲突的元素,只有当他的位置与他本应该在的位置(即%n的位置)之间放满了元素时他才可以放,也就是才可以压入优先队列.所以,当某个元素安放好之后,他后面的原来不能安放的元素是否可以因为他的安放而得到安放的机会.所以我们用并查集维护把安放的位置的父亲设置为他下一位置的父亲,这样当我们判断某个元素可不可以安放的时候只需要看看他与他本应该在的位置(即%n的位置)是不是一个父亲即可~是的话就压入队列~  

代码:

#include
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
const double eps = 1e-12;
const int inf = 0x3f3f3f3f;
map::iterator it;

struct node
{
    int val;
    int pos;
    node(){}
    node (int val,int pos):val(val),pos(pos){}
    friend bool operator < (node x,node y)
    {
    	return x.val> y.val;
	}
};

int n;
int a[maxn],pre[maxn],vis[maxn],ans[maxn];

int find(int x)
{
	return pre[x] == x?x:pre[x] = find(pre[x]);
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		mem(vis,0);
		priority_queue q;
		scanf("%d",&n);
		for(int i = 0;i< n;i++)
			scanf("%d",&a[i]),pre[i] = i;
		
		int num = 0;
		for(int i = 0;i< n;i++)
		{
			if(a[i] == -1) continue;
			if(a[i]%n == i)
			{
				q.push(node(a[i],i));//压入队列
				vis[i] = 1;//已经在队列了
			}
			num++;
		}
		
		int cnt = 0;
		while(!q.empty())
		{
			node tmp = q.top();
			q.pop();
			ans[++cnt] = tmp.val;
			pre[find(tmp.pos)] = find((tmp.pos+1)%n);//连接起来
			int np = pre[tmp.pos];//np为要安放的位置
			if(vis[np]||a[np] == -1||find(a[np]%n)!= np) continue;//a[np]%n 本应该在的位置
			q.push(node(a[np],np));
			vis[np] = 1;
		}
		
		if(cnt< num)
			printf("-1\n");
		else if(cnt == 0) printf("\n");
		else
		{
			for(int i = 1;i< cnt;i++)
				printf("%d ",ans[i]);
			printf("%d\n",ans[cnt]);
		}
	}
	
	return 0;
}

 

你可能感兴趣的:(思维乱搞,并查集)