[JLOI2011]不重复数字

嘟嘟嘟

 

此题看起来不难。

首先我想到的是用map,然而只能的70分,剩下的超时了。然后我就想到了用去重函数unique,这样就过了。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<stack>
 8 #include<queue>
 9 #include<vector>
10 #include<cctype>
11 #include<map>
12 using namespace std;
13 #define enter puts("")
14 #define space putchar(' ')
15 #define Mem(a) memset(a, 0, sizeof(a))
16 typedef long long ll;
17 typedef double db;
18 const int INF = 0x3f3f3f3f;
19 const db eps = 1e-8;
20 const int maxn = 5e4 + 5;
21 inline ll read()
22 {
23     ll ans = 0;
24     char ch = getchar(), last = ' ';
25     while(!isdigit(ch)) {last = ch; ch = getchar();}
26     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
27     if(last == '-') ans = -ans;
28     return ans;
29 }
30 inline void write(ll x)
31 {
32     if(x < 0) putchar('-'), x = -x;
33     if(x >= 10) write(x / 10);
34     putchar(x % 10 + '0');
35 }
36 
37 struct Node
38 {
39     int x, id;
40     bool operator < (const Node& other)const
41     {
42         return id < other.id || (id == other.id && x < other.x);
43     }
44     bool operator == (const Node& other)const    //unique要用的 
45     {
46         return x == other.x;
47     }
48 }a[maxn];
49 
50 bool cmp(Node a, Node b)
51 {
52     return a.x < b.x || (a.x == b.x && a.id < b.id);
53 }
54 
55 int main()
56 {
57     int T = read();
58     while(T--)
59     {
60         int n = read();
61         for(int i = 1; i <= n; ++i) a[i].x = read(), a[i].id = i;
62         sort(a + 1, a + n + 1, cmp);        //按cmp排序 
63         int _n = unique(a + 1, a + n + 1) - a - 1;
64         sort(a + 1, a + _n + 1);            //按结构体自己的排序 
65         for(int i = 1; i <= _n; ++i) write(a[i].x), space; enter;
66     }
67     return 0;
68 }
View Code

 

posted @ 2018-08-21 10:52  mrclr  阅读(163)  评论(0编辑  收藏  举报