Codeforces 1315C Restoring Permutation
You are given a sequence b1,b2,…,bnb1,b2,…,bn . Find the lexicographically minimal permutation a1,a2,…,a2na1,a2,…,a2n such that bi=min(a2i−1,a2i)bi=min(a2i−1,a2i) , or determine that it is impossible.
Each test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤1001≤t≤100 ).
The first line of each test case consists of one integer nn — the number of elements in the sequence bb (1≤n≤1001≤n≤100 ).
The second line of each test case consists of nn different integers b1,…,bnb1,…,bn — elements of the sequence bb (1≤bi≤2n1≤bi≤2n ).
It is guaranteed that the sum of nn by all test cases doesn't exceed 100100 .
For each test case, if there is no appropriate permutation, print one number −1−1 .
Otherwise, print 2n2n integers a1,…,a2na1,…,a2n — required lexicographically minimal permutation of numbers from 11 to 2n2n .
5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8
1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
题意很简单。因为有要求:1.bi=min(a2i−1,a2i),2.字典序尽可能
#include <bits/stdc++.h> using namespace std; int b[205]; int out[205]; bool vis[205]; int main() { int t; cin>>t; while(t--) { int n; cin>>n; int i,j; int flag=1; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { scanf("%d",&b[i]); vis[b[i]]=1; out[2*i-1]=b[i]; } for(i=1;i<=n;i++) { int find=0; for(j=1;j<=2*n;j++) { if(vis[j])continue; if(j>out[2*i-1]) { out[2*i]=j; find=1; vis[j]=1; break; } } if(!find) { flag=0; break; } } if(!flag) { cout<<-1<<endl; continue; } for(i=1;i<=2*n;i++) { cout<<out[i]<<' '; } cout<<endl; } return 0; }
小。所以肯定要把bi放到2i-1的位置(观察样例也不难看出)。然后就是在剩下的数里从小到大地找,找到第一个比bi大的数填到a2i的位置,没有这么一个数的话返回-1.至于正确性的话可以这么想,假设b数组里靠前的是比较小的数,那么从剩下的数里从小到大地选,能保证剩下的里较大的数留给后面更大的bi;假设b数组里靠前的是比较大的数,挑过以后肯定还能保证后面较小的bi有数和它搭配,所以贪心是正确的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!