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

B. Box

Permutation p is a sequence of integers p=[p1,p2,…,pn], consisting of n distinct (unique) positive integers between 1 and n, inclusive. For example, the following sequences are permutations: [3,4,1,2], [1], [1,2]. The following sequences are not permutations: [0], [1,2,1], [2,3], [0,1,2].

The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation p of length n.

You don‘t know this permutation, you only know the array q of prefix maximums of this permutation. Formally:

q1=p1,
q2=max(p1,p2),
q3=max(p1,p2,p3),
...
qn=max(p1,p2,…,pn).
You want to construct any possible suitable permutation (i.e. any such permutation, that calculated q for this permutation is equal to the given array).

Input

The first line contains integer number t (1≤t≤104) — the number of test cases in the input. Then t test cases follow.

The first line of a test case contains one integer n (1≤n≤105) — the number of elements in the secret code permutation p.

The second line of a test case contains n integers q1,q2,…,qn (1≤qi≤n) — elements of the array q for secret permutation. It is guaranteed that qi≤qi+1 for all i (1≤i<n).

The sum of all values n over all the test cases in the input doesn‘t exceed 105.

Output

For each test case, print:

If it‘s impossible to find such a permutation p, print "-1" (without quotes).
Otherwise, print n distinct integers p1,p2,…,pn (1≤pi≤n). If there are multiple possible answers, you can print any of them.

Example

input
4
5
1 3 4 5 5
4
1 1 3 4
2
2 2
1
1
output
1 3 4 5 2
-1
2 1
1

Note

In the first test case of the example answer [1,3,4,5,2] is the only possible answer:

q1=p1=1;
q2=max(p1,p2)=3;
q3=max(p1,p2,p3)=4;
q4=max(p1,p2,p3,p4)=5;
q5=max(p1,p2,p3,p4,p5)=5.
It can be proved that there are no answers for the second test case of the example.

题意

现在给你前缀最大值是多少,让你还原这个排列,问你是否有解。

题解

给了你前缀最大值,我们现在如果发现前缀最大值变化了,那么这个位置肯定是这个最大值,否则就插入了一个小的数,那么我们插入最小的就好。

代码

#include<bits/stdc++.h>
using namespace std;

vector<int>Q;
void solve(){
    int n;scanf("%d",&n);
    Q.clear();
    vector<int> ans;
    set<int>S;
    for(int i=0;i<n;i++){
        int x;scanf("%d",&x);
        Q.push_back(x);
        S.insert(i+1);
    }
    int mx = 0;
    for(int i=0;i<n;i++){
        if(Q[i]>mx){
            if(S.count(Q[i])){
                S.erase(Q[i]);
                ans.push_back(Q[i]);
            }else{
                cout<<"-1"<<endl;
                return;
            }
            mx = Q[i];
        }else{
            if(*S.begin()>mx){
                cout<<"-1"<<endl;
                return;
            }else{
                ans.push_back(*S.begin());
                S.erase(S.begin());
            }
        }
    }
    for(int i=0;i<ans.size();i++){
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        solve();
    }
}

原文地址:https://www.cnblogs.com/qscqesze/p/11925055.html

时间: 2024-07-30 07:58:29

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B. Box 贪心的相关文章

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

A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最长我们肯定是取最小x和最大的y连起来就完事. 但是要求长度最小又得覆盖,那么可以这样想,我们需要把最小的x不断右移到这条线段的y, 最大的左移到x,所以就是最大x-最小y完事 #include <bits/stdc++.h> using namespace std; #define ll long

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造

C. Messy You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence s=s1s2-sn of length n. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choo

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) F2. Wrong Answer on test 233 (Hard Version) dp 数学

F2. Wrong Answer on test 233 (Hard Version) Your program fails again. This time it gets "Wrong answer on test 233" . This is the harder version of the problem. In this version, 1≤n≤2?105. You can hack this problem if you locked it. But you can h

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心

D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, 1≤n,m≤2?105. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems. You are given a seque

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A Math Problem

//只要从所有区间右端点的最小值覆盖到所有区间左端点的最大值即可 #include<iostream> using namespace std ; int x,y; int n; int t; int main() { cin>>t; while(t--) { cin>>n; if(n==1) { cin>>x>>y; cout<<0<<endl; } else { int l=0,r=1e9+10; for(int i=

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++

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

//因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> using namespace std ; typedef pair<int,int>PII; int n,k; string s; string get_str(int n,int k) {//先构建前k-1个 string res=""; for(int i=0; i<k

【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B:数学+排序 C:字符串搜索 A // https://codeforces.com/contest/1277/problem/A /* 题意: 给出一个数,求不大于该数的完美整数的个数(完美整数指全是一个数组成的数字,如:111, 333333, 444444, 9, 8888 ...) 分析:

Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) 题解

A..B略 C 对当前的值排序,再二分答案,然后对于(i%x==0 && i%y==0)放入大的,再放其他的贪心解决即可. #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<map> #define LL long long #define lson rt<<1 #define rson rt<