- 答疑
P4305 [JLOI2011] 不重复数字
- 2025-8-24 22:54:47 @
P4305 [JLOI2011] 不重复数字
题意概括
给定 组测试数据。对于每组数据,给定一个长度为 的整数序列,要求从左到右遍历序列,去除所有重复出现的数字,只保留每个数字第一次出现时的样子,并按原顺序输出。
数据范围:
- 序列中的每个数都在
int
范围内。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
unordered_set<int> s;
void sol() {
s.clear();
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
// s.count(x) 查询x是否存在,复杂度平均O(1)
if (!s.count(x)) {
cout << x << " ";
s.insert(x); // 插入新元素
}
}
cout << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
sol();
}
return 0;
}
0 条评论
目前还没有评论...