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.

题意

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

题解

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

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int>Q;
  4. void solve(){
  5. int n;scanf("%d",&n);
  6. Q.clear();
  7. vector<int> ans;
  8. set<int>S;
  9. for(int i=0;i<n;i++){
  10. int x;scanf("%d",&x);
  11. Q.push_back(x);
  12. S.insert(i+1);
  13. }
  14. int mx = 0;
  15. for(int i=0;i<n;i++){
  16. if(Q[i]>mx){
  17. if(S.count(Q[i])){
  18. S.erase(Q[i]);
  19. ans.push_back(Q[i]);
  20. }else{
  21. cout<<"-1"<<endl;
  22. return;
  23. }
  24. mx = Q[i];
  25. }else{
  26. if(*S.begin()>mx){
  27. cout<<"-1"<<endl;
  28. return;
  29. }else{
  30. ans.push_back(*S.begin());
  31. S.erase(S.begin());
  32. }
  33. }
  34. }
  35. for(int i=0;i<ans.size();i++){
  36. cout<<ans[i]<<" ";
  37. }
  38. cout<<endl;
  39. }
  40. int main(){
  41. int t;
  42. scanf("%d",&t);
  43. while(t--){
  44. solve();
  45. }
  46. }

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

  1. 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) 思路:我们想一下如果题目说的是最 ...

  2. 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 ...

  3. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) E. Arson In Berland Forest 二分 前缀和

    E. Arson In Berland Forest The Berland Forest can be represented as an infinite cell plane. Every ce ...

  4. 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, ...

  5. 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 ...

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

    A. Math Problem Your math teacher gave you the following problem: There are n segments on the x-axis ...

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

    //因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...

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

    #include<bits/stdc++.h> using namespace std; ]; ]; int main() { int total; cin>>total; w ...

  9. 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 ...

随机推荐

  1. [document.cookie]为什么cookie不在window下的呢.奇怪了[未完待续]

    什么是cookie,怎么就叫cookis,它能干嘛 我猜吧,就是登录页面的时候传值,二次登录的时候可以给你说句'hello xxx'; 下面这堆比较啰嗦,随意看吧 //cookie 用户储存在用户本地 ...

  2. 从0系统学Android--4.1探究碎片

    从0系统学Android--4.1探究碎片 本系列文章目录:更多精品文章分类 本系列持续更新中.... 初级阶段内容参考<第一行代码> 第四章:手机平板要兼顾--探究碎片 平板电脑和手机最 ...

  3. DSP开发程序相关问题总结

    1. 定义Class总是出错,原来是这样的class SCM_DRV_API CSERCOS{}:后来改为class CSERCOS{}:就可以了. 类的一般定义格式如下:    class < ...

  4. SpringCloudGateway开发详解

    路由简介: SpringCloudGateWay 是用于替代zuul作为API网关,在gateway中有三个重要的名词:过滤器,断言,路由 过滤器与断言是路由的一部分,路由便是将请求进行一系列的处理后 ...

  5. web项目踩坑过程

    sql函数设计: 一开始本来是直接用Java的jdbc直接传输操作语句的.但后来学了存储过程发现存储过程可以提高不少的效率.就重构了自己对数据库的操作代码.包括:开启,查找,修改,关闭. 开启:直接使 ...

  6. 如何在Oracle 12C中添加多个分区 (Doc ID 1482456.1)

    How to Add Multiple Partitions in Oracle 12C (Doc ID 1482456.1) APPLIES TO: Oracle Database - Enterp ...

  7. USB OTG ID 检测原理【转】

    OTG 检测的原理是: USB OTG标准在完全兼容USB2.0标准的基础上,增添了电源管理(节省功耗)功能,它允许设备既可作为主机,也可作为外设操作(两用OTG).USB OTG技术可实现没有主机时 ...

  8. Java之IO初识(字节流和字符流)

    IO概述 生活中,你肯定经历过这样的场景.当你编辑一个文本文件,忘记了 ctrl+s ,可能文件就白白编辑了.当你电脑上插入一个U盘,可以把一个视频,拷贝到你的电脑硬盘里.那么数据都是在哪些设备上的呢 ...

  9. 【linux命令 】文件特殊权限(SUID、SGID、SBIT)

    chmod 2770 /home/admins,刚看到这个命令,有点不解,后边770分别表示用户,组,其他人,前面的2不知道代表的是什么意思.百度之后发现2是代表八进制数,也是一种权限,它的三个bit ...

  10. new String()与toString

    str.toString是调用了str这个object对象的类的toString方法.一般是返回这么一个String:[class name]@[hashCode].new String(str)是根 ...