Bzoj 2761(STL)

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

#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;
}
------ 本文结束 ------