bzoj2761[JLOI2011]不重复数字

bzoj2761[JLOI2011]不重复数字

题意:

给出N个数,要求把其中重复的去掉,只保留第一次出现的数。n≤50000

题解:

一道令管理员都后悔加入的水题,按大小排序后unique,再按读入顺序排序即可。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define inc(i,j,k) for(int i=j;i<=k;i++)
 5 using namespace std;
 6 
 7 inline int read(){
 8     char ch=getchar(); int f=1,x=0;
 9     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
10     return f*x;
11 }
12 int t,n,tot;
13 struct nd{int v,id;};
14 bool cmp1(nd a,nd b){if(a.v!=b.v)return a.v<b.v;else return a.id<b.id;}
15 bool cmp2(nd a,nd b){return a.id<b.id;};
16 nd a[50010],b[50010];
17 int main(){
18     t=read(); while(t--){
19         n=read(); inc(i,1,n)a[i].v=read(),a[i].id=i; sort(a+1,a+n+1,cmp1); tot=0;
20         inc(i,1,n)if(i==1||a[i].v!=a[i-1].v)b[++tot]=a[i]; sort(b+1,b+tot+1,cmp2);
21         inc(i,1,tot-1)printf("%d ",b[i].v); printf("%d\n",b[tot].v);
22     }
23     return 0;
24 }

 

20160610

posted @ 2016-08-16 23:02  YuanZiming  阅读(198)  评论(0编辑  收藏  举报