牛客网暑期ACM多校训练营(第四场)J Hash Function(拓扑排序思想)

链接:https://www.nowcoder.com/acm/contest/142/J
来源:牛客网
 

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

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

题意:给你一个按a[i] mod n方式的hash表,处理冲突的方法是找(a[i]+1) mod n 直到找到一个空的位置(空的位置为-1)。

思路:很容易想到拓扑排序。而且这里需要字典序最小,显然是用队头元素最小的优先队列。但是本题用的是拓扑排序的思想。

如果a[i]%n==i 说明在(mod n==i的位置下,第一个插入的就是该数,于是加入到队列中。为了保证存在序列,我们用一个tmp计数,看不为-1的元素有几个,就是原序列有几个数。)

然后依次从队列中取出最小的元素,找它下标(同时存值和下标,用pair快)的前一个未被加入到答案的位置l到后一个未被加入到答案的位置r。如果后一个未被加入答案的位置r不在当前队列中且不为-1且(r-a[i]%n+n)%n<=(n+r-l-1)%n 则表示a[r]这个位置之前到a[i]%n==i==a[r]%n的位置都已经被填满,因此这个数就可以填了。于是把(a[r],r)加入到优先队列中。这样用vis数组记录哪个下标被加入到了答案序列中,us数组记录哪个下标被加入过优先队列中,记录答案序列的个数f,若f小于tmp则无法完全拓扑,输出-1。

注意:本题有n=0的情况需要特判直接输出回车,不然会格式错误(PE),包括我在内的5页的人都PE了几发。。。

代码(目前是通过的代码中最短的):

#include
#define ll long long
#define inf 0x3f3f3f3f
#define p pair
using namespace std;
const int maxn=200010;
int n,m,k,cnt,tmp,T,f,t;
int a[maxn],vis[maxn],us[maxn],pre[maxn],nex[maxn],ans[maxn];
int main()
{
    scanf("%d",&T);
    while(T--){
     priority_queue,greater

>q; scanf("%d",&n); for(int i=0;i<=n;i++)vis[i]=us[i]=pre[i]=nex[i]=0; tmp=0; for(int i=0;i=(n+r-a[r]%n)%n) { q.push(p(a[r],r)); us[r]=1; } } if(f!=tmp)puts("-1"); else { if(!f) puts(""); else for(int i=0;i

 

你可能感兴趣的:(数据结构:队列,图论:拓扑排序)