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();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2016-11-24 Codeforces Round #381 (Div. 1) A. Alyona and mex 构造
2016-11-24 BZOJ 2648: SJY摆棋子 kdtree
2015-11-24 线段树 模板
2015-11-24 Codeforces Round #115 B. Plane of Tanks: Pro 水题
2015-11-24 Codeforces Round #115 A. Robot Bicorn Attack 暴力