[codeforces 1385B] Restore the Permutation by Merger 寻找没有打过标记的数

Codeforces Round #656 (Div. 3)   参与排名人数11542   早睡早起身体好

[codeforces 1385B]   Restore the Permutation by Merger   寻找没有打过标记的数

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1385/problem/B

Problem Lang Verdict Time Memory
B - Restore the Permutation by Merger GNU C++17 Accepted 31 ms 3600 KB

题目大意:给定一个排列数的数组,让同样的排列数数组,与其混合,要求不改变元素间的相对位置,请根据混合后的数组,找寻原来的排列数数组。

样例模拟如下:

4
1 3 1 4 3 4 2 2

位置1 2 3 4 5 6 7 8
数值1 3 1 4 3 4 2 2

自左向右遍历,
1.找到1,打上标记,输出1.
2.找到3,打上标记,输出3.
3.找到1,发现已经打过标记.
4.找到4,打上标记,输出4.
5.找到3,发现已经打过标记.
6.找到4,发现已经打过标记.
7.找到2,打上标记,输出2.
8.找到2,发现已经打过标记.

AC代码如下:

#include 
int a[110],vis[55];
int main(){
	int t,n,i;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(i=1;i<=2*n;i++)scanf("%d",&a[i]);
		for(i=1;i<=n;i++)vis[i]=0;//标记初始化
		for(i=1;i<=2*n;i++)
			if(!vis[a[i]]){
				printf("%d ",a[i]);//打印未被标记过的数
				vis[a[i]]=1;
			}
		printf("\n");
	}
	return 0;
}

 

你可能感兴趣的:(codeforces)