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();
}
}
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) 思路:我们想一下如果题目说的是最 ...
- 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 ...
- 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 ...
- 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, ...
- 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 ...
- 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 ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy
//因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...
- 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 ...
- 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 ...
随机推荐
- [转]UiPath Invoke Code
本文转自:https://dotnetbasic.com/2019/08/uipath-invoke-code.html We will learn step by step tutorial for ...
- java核心API
---恢复内容开始--- Javase01 day01 关于String: String是不可变对象,java.lang.String使用了final修饰,不能被继承: 字符串一旦创建永远无法改变,但 ...
- SpringCloud之Zuul:服务网关
Zuul在Web项目中的使用见上文<SpringBoot中使用Zuul>,下面例子为Zuul在Spring Cloud的使用. 开发工具:IntelliJ IDEA 2019.2.3 一. ...
- python科学计算和数据分析常用库
NumPy NumPy最强大的是n维数组,该库还包含基本的线性代数函数.傅立叶变换.随机函数和其他底层语言(如Fortran.C和C++)集成的工具. SciPy SciPy建立在NumPy基础上,它 ...
- [PHP] swoole直接使用二进制包
swoole提供一个编译好的二进制包,这个包连php都包含进去了,下载解压后就可以直接运行,都不用安装php 在这个地方直接下载二进制包 https://www.swoole.com/page/dow ...
- Self Service Password 密码策略
1.在活动目录中新建一个用户,并赋予域管理员权限:2.拷贝conf目录下的config.inc.php为config.inc.local.php:3.按自己的实际情况及要求修改config.inc.l ...
- Nginx 简介与安装、常用的命令和配置文件
1.nginx 简介(1)介绍 nginx 的应用场景和具体可以做什么事情 (2)介绍什么是反向代理 (3)介绍什么是负载均衡 (4)介绍什么是动静分离 2.nginx 安装(1)介绍 nginx 在 ...
- 005.SQLServer AlwaysOn可用性组高可用简介
一 AlwaysOn 可用性组 1.1 AlwaysOn 可用性组概述 AlwaysOn 可用性组功能是一个提供替代数据库镜像的企业级方案的高可用性和灾难恢复解决方案.SQL Server 2012 ...
- 斐波那契数列(Java)
一.什么是斐波那契数列 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为& ...
- git 分布式控制版本管理器(上)
git的作用: 1.更方便的存储版本 2.恢复之前的版本 3.更方便的对比 4.协同合作 下载地址git官网: https://git-scm.com/ 可自选自己电脑的操作系统 安装: 一路next ...