牛客网暑期ACM多校训练营(第四场) J Hash Function /CCPC-Wannafly Winter Camp Day7 (Div2, onsite) E 线性探查法

链接:https://ac.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 h(x)=xmodnh(x)=xmodn.
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 (h(x)+1)modn(h(x)+1)modn 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线性探测之后的表,让你求字典序最小的原序列。(两道题差别只在输入和-1,后一道题保证了没有-1的情况)

思路:类似于拓扑排序的做法。首先将a[i]%n==i的元素入队,因为它显然是hash中直接填入的数字。

然后由于字典序最小,每次我们取出一个值最小的元素填入相应位置,然后更新这个位置的前驱和后继(这个位置前面和后面最靠近这个位置的没填数的位置(不包括已经入队元素的位置,而是确定未填数的位置),然后对于它的后继,判断是否位置已经被占用,并且判断前驱到后继这一段数是否已经被填满,如果填满那么这个位置就可以填上了),注意细节。

如果最终填入的数字个数少于非-1数字个数,则显然原序列不存在。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define p pair<int,int>
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()
{
    T=1;
    while(T--){
     priority_queue<p,vector<p>,greater<p> >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;i++)
     {
         scanf("%d",&a[i]);
         if(a[i]!=-1&&a[i]%n==i){q.push(p(a[i],i));us[i]=1;}
         if(a[i]!=-1)tmp++;
     }
     cnt=0;f=0;
     while(!q.empty())
     {
         p k=q.top();q.pop();
         t=k.second;
         ans[f++]=a[t];
         if(f==tmp) break;
         vis[t]=1;
         int r=(t+1)%n,l=(t-1+n)%n;
         while(vis[r])r=nex[r];
         while(vis[l])l=pre[l];
         nex[t]=r;pre[t]=l;
         if(!us[r]&&a[r]!=-1&&(n+r-l-1)%n>=(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<f;i++)
     printf("%d%c",ans[i],i==f-1?'\n':' ');
    }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值