[ABC306C] Centers
题目分析:
- 用一个标记数组储存每个数出现的次数,
当第二次出现时用记录权值和位置。按位置从小到大排序。最后输出权值。
代码:
#include <bits/stdc++.h>
using namespace std;
template <typename _T>
inline void read(_T &x)
{
x = 0; char c; _T d = 1;
while(c > 57 || c < 48){
c = getchar();
if(c == '-') d = -1;
}
while(c >= 48 && c <= 57){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
x *= d;
}
const int N = 3e5 + 5;
int n;
int a[N], t[N];
struct Node{
int pos, val;
bool operator <(const Node & other) const {
return pos < other.pos;
}
}ans[N];
signed main()
{
read(n);
for(int i = 1; i <= 3 * n; i++){
read(a[i]);
if(t[a[i]] == 1){
ans[a[i]].pos = i;
ans[a[i]].val = a[i];
}
t[a[i]]++;
}
sort(ans + 1, ans + 1 + n);
for(int i = 1; i <= n; i++){
cout << ans[i].val << " ";
}
return 0;
}
正文完