牛客网暑期ACM多校训练营(第四场)J Hash Function

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

题目描述

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

这题解法十分巧妙 用并查集记寻找这个数后的第一个不在本位置的数 并且加入队列

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
vector<int>ans;
int a[maxn],vis[maxn],par[maxn];
struct node
{
    int v,id;
    node (int v,int id):v(v),id(id){}
    bool operator < (const node &other)const{return v>other.v;}
};
int Find(int x){return x==par[x]?x:par[x]=Find(par[x]);}
void Init(int n){for(int i=0;i<=n;i++)par[i]=i,vis[i]=0;}
priority_queue<node>q;
int main()
{
    int n,T;
    scanf("%d",&T);
    while(T--)
    {
        int cnt=0,flag=0;
        scanf("%d",&n);Init(n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]!=-1) cnt++;
            if(a[i]!=-1&&a[i]%n==i) q.push(node(a[i],i)),flag=1,vis[i]=1;
        }
        if(cnt==0){printf("\n");continue;} //如果全是-1则没有序列输出
        if(flag==0){printf("-1\n");continue;} //如果非全是-1且没有符合的序列 输出-1
        while(!q.empty())
        {
            node head=q.top();q.pop();
            ans.push_back(head.v);
            if(ans.size()==cnt)break; //如果已经凑齐了答案则跳出
            par[head.id]=(head.id+1)%n; //将下一位放入并查集
            int k=Find(head.id); //通过连续的数列到末端
            if(vis[k]||a[k]==-1)continue; //判断末端是否为空且末端必须为不在本位的数
            if(Find(a[k]%n)==k) //如果末端上所在的数被移动到末端则成立
            {
                q.push(node(a[k],k)); 
                vis[k]=1;
            }
        }
        if(ans.size()!=cnt)
        {
            printf("-1\n");
        }
        else
        {
            for(int i=0;i<ans.size();i++)
            {
                printf("%d%c",ans[i],i==ans.size()-1?'\n':' ');
            }
        }
        while(!q.empty())q.pop();
        ans.clear();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ljq199926/article/details/81279918