Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) 比赛人数3742
[codeforces 1315C] Restoring Permutation 比赛很锻炼人 做好最小匹配
总目录详见https://blog.csdn.net/mrcrack/article/details/103564004
在线测评地址https://codeforces.ml/contest/1315/problem/C
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
C - Restoring Permutation | GNU C++11 | Accepted | 31 ms | 0 KB |
比赛时,第一想法,觉得判定if there is no appropriate permutation, print one number −1.比较困难,在判定上,耗了比较多时间,
随着深入的思考,感觉,编码困难重重,比赛时,很难完成,果断换思路。
在比赛比较短的时间里,能这么快的作出思维转换,感觉收获到了信心。
以往是根本不具备这个实力,一条路走到黑。
继续想,只要个题中的每个数据,配上合理的刚好只大一点数据,若最后,还有数据找不到合适的配对数据,打印-1.
样例手动算法演示如下
5
1
1
配对
(1,2)
输出
1 2
2
4 1
配对
(4,0)(1,2) 注意:4无法配对成功
输出
-1
3
4 1 3
配对
(4,5)(1,2)(3,6)
输出
4 5 1 2 3 6
4
2 3 4 5
配对
(2,6)(3,7)(4,8)(5,0) 注意:5无法配对成功
输出
-1
5
1 5 7 2 8
配对
(1,3)(5,6)(7,9)(2,4)(8,10)
输出
-1
1 3 5 6 7 9 2 4 8 10
比赛中,还出了些小状况,没有看到required lexicographically minimal permutation of numbers from 1 to 2n.
提交代码,Wrong answer on pretest 1,不过很快纠正,以下为AC代码。
#include <cstdio>
#include <cstring>
using namespace std;
struct node{
int v,other;//other配对的值
}b[105];
int vis[205];//标记用过的数据
int main(){
int t,n,i,j,m,flag;
scanf("%d",&t);
while(t--){
scanf("%d",&n),m=n*2,memset(vis,0,sizeof(vis)),memset(b,0,sizeof(b));
for(i=1;i<=n;i++)scanf("%d",&b[i].v),vis[b[i].v]=1;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++){
if(!vis[j]&&b[i].v<j){//没用过,且能配对的数j
b[i].other=j;
vis[j]=1;
break;
}
}
flag=0;
for(i=1;i<=n;i++)
if(!b[i].other){//other==0,表明有数据,没有配对成功
printf("-1\n");
flag=1;
break;
}
if(!flag){//所有数据配对成功
for(i=1;i<=n;i++)
printf("%d %d ",b[i].v,b[i].other);
printf("\n");
}
}
return 0;
}