Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B Box

#include<bits/stdc++.h>
using namespace std;
int shu[100005];
int ans[100005];
int main() {
    int total;
    cin>>total;
    while(total--) {
        int n;
        bool flag=true;
        cin>>n;
        unordered_set<int>tree;
        set<int>num;
        for(int i=1; i<=n; i++) num.insert(i);//插入每个数字   从小到大 
        for(int i=1; i<=n; i++) cin>>shu[i];//记录每个数字 
        for(int i=1; i<=n; i++) {
            if(tree.find(shu[i])==tree.end()) {//如果当前位置的最大值没有出现过,那么该最大值在此位置第一次出现 
                tree.insert(shu[i]);//插入 
                num.erase(shu[i]);//删除 
                ans[i]=shu[i];//记录 
            } else//如果当前位置的最大值出现过 ,就从小于该最大值的数字中随便选一个没用的 
            {
                for(auto j=num.begin(); j!=num.end(); j++) {
                    if((*j)<shu[i])// 如果有 
                     {
                        ans[i]=(*j);//加进去 
                        num.erase(j);//删掉 
                        break;//推出循环 
                    } else if(*j>shu[i]) {//如果最小的都大于, 
                        flag=false;
                        break;
                    }
                }
                if(!flag) break;
            }
        }
        if(!flag) cout<<-1<<endl;
        else {
            for(int i=1; i<=n; i++) {
                cout<<ans[i]<<" ";
            }
            cout<<endl;
        }

    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/QingyuYYYYY/p/11964983.html