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
给定一个Hash数列,求原数列,要求最后输出的字典序最小。
Emmmmmm,最近不wa10+都不好意思过题,模拟过的,看其他大佬还有并查集,拓扑过的,感觉思维跟不上,我也就做个模拟题了。
首先发现当s[i]%n == i的时候,说明这个数是第一次就满足原序列要求的,所以先把所有这类数字归到一个集合中去,然后去找此时的待选集合中最小数字的下一个空位置,也就是未进入集合的位置,判断他是否可以进入集合,最开始待选集合中只有 s[i]% n == i这类数字的最小值s[i],不断向其中加数字,维护每次取集合数的最小值,得到答案。
维护集合数的最小值可以用优先队列维护,每次找此数字的下一个空位置可以将下标放入set维护,刚开始可以把s[i]%n == i的数全部放入优先队列,这样不用找到那个最小值,后面判断一下,不重复进入队列就ok。
代码实现:
/*
Look at the star
Look at the shine for U
*/
#include
#define sl(x) scanf("%lld",&x)
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const ll mod = 1e9+7;
const ll INF = 1e18;
int ans[N],cnt;
struct node{
int num,index;
friend bool operator < (node f1, node f2)
{
if(f1.num == f2.num) return f1.index > f2.index;
return f1.num > f2.num;
}
}p[N];
int main()
{
int n,i,j,k,t;
scanf("%d",&t);
while(t--)
{
set s;
scanf("%d",&n);
cnt = 0;k = 0;
priority_queue q;
for(i = 0;i < n;i++)
{
scanf("%d",&p[i].num);p[i].index = i;
if(p[i].num%n == i) q.push(p[i]);
if(p[i].num != -1) k++;
s.insert(i);
}
set ::iterator it;
set ::iterator lt;
while(!q.empty())
{
node temp = q.top(); q.pop();
if(!s.count(temp.index)) continue;
ans[cnt++] = p[temp.index].num;
s.erase(temp.index);
/* -------pay attention-------*/
if(!s.empty())
{
it = s.upper_bound(temp.index);
if(it == s.end()) it = s.begin();
lt = s.lower_bound((p[*it].num)%n);
if(lt == s.end()) lt = s.begin();
if(lt == it && p[*it].num != -1) q.push(p[*it]);
}
}
if(cnt != k) puts("-1");
else
{
for(i = 0;i < cnt;i++)
{
if(i) printf(" "); printf("%d",ans[i]);
}
puts("");
}
}
}
/*
3 3
-1 1 1
*/