P4305 [JLOI2011]不重复数字

题面

给定 \(n\) 个数,要求把其中重复的去掉,只保留第一次出现的数。

思路

暴力大法好!

STL大法好!

直接使用std::unorded_map维护(哈希表),判断是否出现即可。每组数据的时间复杂度是 \(O(n)\)

代码

需要输入输出优化,否则只有 \(60\) 分。

#include <bits/stdc++.h>
using namespace std;

unordered_map<int,int> rem;
int t;


void solve(){
	rem.clear();
	int n,x;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		if(rem.count(x)==0){
			cout<<x<<' ';
			rem[x]=114514;
		}
	}
	cout<<'\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

AC记录

posted @ 2022-03-25 22:12  蒟蒻xiezheyuan  阅读(64)  评论(0编辑  收藏  举报