0
点赞
收藏
分享

微信扫一扫

B. Restore the Permutation by Merger(序列问题+统计)Codeforces Round #656 (Div. 3)

原题链接:​​https://codeforces.com/contest/1385/problem/B​​

题意:有一个置换p[1…n]。它与自己合并了。换句话说,让我们取p的两个实例,并将第二个p的元素插入到第一个保持元素相对顺序的元素中。结果是一个长度为2n的序列。我们给定一个B. Restore the Permutation by Merger(序列问题+统计)Codeforces Round #656 (Div. 3)_序列的序列,求出原置换序列。

解题思路:这题就是要输出原置换序列,且相对顺序并未发生改变,所以我们只要依次存储第一次出现的数字就一定是原序列中的顺序。我们利用辅助数组visited来判断是否是第一次出现。具体看代码。

AC代码:

/*
*
*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
int t,n;//t组测试数据,n为置换长度。
bool visited[55];//利用此来判断是否统计。
while(cin>>t){
while(t--){
cin>>n;
int temp;
memset(visited,false,sizeof(visited));
vector<int> nums;
rep(i,1,2*n){
cin>>temp;
if(!visited[temp]){
nums.push_back(temp);
visited[temp]=true;
}
}
rep(i,0,n-1){
cout<<nums[i];
if(i!=n-1)
cout<<" ";
}
cout<<endl;
}
}
return 0;
}


举报

相关推荐

0 条评论