牛客多校第四场 J题

链接: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;
typedef pair<int,int > pii;
const int N =2e5+5;
int f[N];
int vis[N];
int a[N];
int n;

inline int getf(int x)
{
    return f[x]==x?x:(f[x]=getf(f[x]));
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            f[i]=i;
            vis[i]=0;
        }

        int cnt=0;
        for(int i=0;i<n;i++){
            if(a[i]!=-1){
                cnt++;
            }
        }
        if(cnt==0){
            printf("\n");
            continue;
        }

        priority_queue< pii,vector<pii >, greater<pii> >q;
        for(int i=0;i<n;i++){
            if(a[i]%n==i){
                q.push(pii(a[i],i));
                vis[i]=1;
            }
        }

        if(q.empty()){
            printf("-1\n");
            continue;
        }

        vector< int >ans;
        while(!q.empty()){
            pii tmp=q.top(); q.pop();
            //cout<<"val "<<tmp.first<<" index "<<tmp.second<<endl;
            ans.push_back(tmp.first);
            if(ans.size()==cnt) break;
            int in=tmp.second;
            f[in]=(in==n-1?0:in+1);
            int fa=getf(in);
            if(vis[fa]||a[fa]==-1) continue;
            if(getf(a[fa]%n)==fa){ // 如果他应该在的位置到现在在的位置全部有数,那么就一定可以加入队列中
                q.push(pii(a[fa],fa));
                vis[fa]=1;
            }
        }

        if(cnt!=ans.size())
        {
            cout<<-1<<endl;
        }
        else{
            cout<<ans[0];
            for(int i=1;i<ans.size();i++){
                cout<<" "<<ans[i];
            }
            cout<<endl;
        }

    }
    return 0;
}

第二种做法: 线段树建图+ 拓扑排序

代码: 

#include<bits/stdc++.h>
#define lson (i<<1)
#define rson (i<<1|1)
 
using namespace std;
const int N =2e5+5;
typedef pair<int ,int > pii;
 
struct node
{
    int l,r;
}tr[N<<2];
 
int pn[N];
int inde[4*N];
vector< int >edge[4*N];
vector< int >ans;
int a[N];
int val[N*4];
int sum[N];
 
void build(int i,int l,int r)
{
    tr[i].l=l; tr[i].r=r;
    if(l==r){
        val[i]=a[l];
        pn[l]=i;
        return ;
    }
    int mid=(tr[i].l+tr[i].r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
    inde[i]+=2;
    edge[lson].push_back(i);
    edge[rson].push_back(i);
}
 
void update(int i,int l,int r,int num)
{
    if(tr[i].l==l&&tr[i].r==r){
        edge[i].push_back(num);
        inde[num]++;
        return ;
    }
    int mid=(tr[i].l+tr[i].r)>>1;
    if(r<=mid) update(lson,l,r,num);
    else if(l>mid) update(rson,l,r,num);
    else
    {
        update(lson,l,mid,num);
        update(rson,mid+1,r,num);
    }
}
 
int n;
 
void init()
{
    for(int i=0;i<=n;i++) sum[i]=0;
    for(int i=0;i<=4*n;i++){
        val[i]=-1;
        inde[i]=0;
    }
    for(int i=1;i<=n*4;i++) edge[i].clear();
}
 
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        init();
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        for(int i=0;i<n;i++){
            if(a[i]!=-1) sum[i]=1;
            //sum[i]=sum[i-1]+sum[i];
        }
        for (int i = n - 1; i >= 0; --i)
        {
            sum[i] += sum[i + 1];
        }
 
        bool valid = true;
        for (int i = 0; i < n; ++i)
            if (a[i] != -1 && a[i] % n != i) // 表示不是入度为0 的点
            {
                //cout<<"l&&&&&&"<<endl;
                int x = a[i] % n;
                if (x < i)
                {
                    valid &= (sum[x] - sum[i] == i - x);  //中间是否有-1 存在
                }
                else
                {
                    valid &= (sum[0] - sum[i] == i && sum[x] == n - x);
                }
            }
 
        if (!valid)
        {
            puts("-1");
            continue;
        }
        /*
        int flag=0;
        for(int i=0;i<n;i++){
            if(a[i]==-1) continue;
            if(a[i]%n==i) continue;
            int in1=a[i]%n;
            int cnt=0;
            if(in1<i){
                if(in1==0) cnt=sum[i-1];
                else cnt=sum[i-1]-sum[in1-1];
            }
            else{
                if(i==0){
                    cnt+=sum[n-1]-sum[in1-1];
                }
                else{
                    cnt+=sum[i-1];
                    cnt+=sum[n-1]-sum[in1-1];
                }
            }
            int aimcnt=0;
            if(i<in1){
                aimcnt=i+n-in1;
            }
            else{
                aimcnt=i-in1;
            }
            if(cnt<aimcnt){
                flag=1; break;
            }
        }
        if(flag){
            printf("-1\n");
            continue;
        }
        */
 
        build(1,0,n-1);
 
        priority_queue<pii,vector<pii>,greater<pii> >q;
        for(int i=0;i<n;i++){
            if(a[i]==-1) continue;
            if(a[i]%n==i){
                q.push(pii(a[i],pn[i]));
            }
            else{
                int in1=a[i]%n;
                if(in1<i){
                    update(1,in1,i-1,pn[i]);
                }
                else{
                    if(i!=0){
                        update(1,0,i-1,pn[i]);
                    }
                    update(1,in1,n-1,pn[i]);
                }
            }
        }
 
 
        int cnt=0;
        ans.clear();
        while(!q.empty()){
            pii tmp=q.top(); q.pop();
            if(tmp.first!=-1){
                ans.push_back(tmp.first);
            }
            int u=tmp.second;
            //cout<<"u "<<tmp.second<<" val "<<tmp.first<<endl;
            for(int i=0;i<edge[u].size();i++){
                int v=edge[u][i];
                inde[v]--;
                if(inde[v]==0){
                    q.push(pii(val[v],v));
                }
            }
        }
 
        if(ans.size()!=sum[0]){
            printf("-1\n");
            continue;
        }
        for(int i=0;i<ans.size();i++){
            if(i) printf(" ");
            printf("%d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值