Bzoj 2761(STL)

BZOJ 2761
先按照数值排序,然后unique去重,然后再按编号排序,输出即可。
本题练习STL不错,本题set也能做。
注意:sort的cmp必须加const,unique返回值要记得减

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
const int MAXN = 50000 + 5;
struct data {
int num, v;
bool operator == (const data &b) const {
return v == b.v;
}
}ai[MAXN];
bool cmp1(const data &a, const data &b) {
if (a.v == b.v) return a.num < b.num;
return a.v < b.v;
}
bool cmp2(const data &a, const data &b) {
return a.num < b.num;
}
int n;
void clean() {
}
void solve() {
scanf("%d", &n);
clean();
for (int i = 1; i <= n; i++) {
scanf("%d", &ai[i].v);
ai[i].num = i;
}
sort(ai + 1, ai + 1 + n, cmp1);
int len = unique(ai + 1, ai + 1 + n) - ai;
sort(ai + 1, ai + len, cmp2);
for (int i = 1; i < len; i++) {
printf("%d", ai[i].v);
if (i == len - 1) printf("\n"); else printf(" ");
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);freopen("1.out", "w", stdout);
#endif
int T; scanf("%d", &T);
while (T--) solve();
return 0;
}

------ 本文结束 ------