【JLOI2011】【BZOJ2761】不重复数字

来源:互联网 发布:淘宝官服电话 编辑:程序博客网 时间:2024/05/21 00:20

Description
给出N个数,要求把其中重复的去掉,只保留第一次出现的数。
例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4。

Input
输入第一行为正整数T,表示有T组数据。
接下来每组数据包括两行,第一行为正整数N,表示有N个数。第二行为要去重的N个正整数。

Output

对于每组数据,输出一行,为去重后剩下的数字,数字之间用一个空格隔开。
Sample Input
2
11
1 2 18 3 3 19 2 3 6 5 4
6
1 2 3 4 5 6
Sample Output
1 2 18 3 19 6 5 4
1 2 3 4 5 6
HINT

对于30%的数据,1 <= N <= 100,给出的数不大于100,均为非负整数;

对于50%的数据,1 <= N <= 10000,给出的数不大于10000,均为非负整数;

对于100%的数据,1 <= N <= 50000,给出的数在32位有符号整数范围内。

提示:

由于数据量很大,使用C++的同学请使用scanf和printf来进行输入输出操作,以免浪费不必要的时间。

Source

看见QDEZ一个大爷写这个题我就来写了(其实是因为那个叫slyz的马甲实在瞩目
题傻逼但是有坑点…输入不止正整数,和输入描述不一致

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define MAXN 50100#define MAXINT 0x7fffffffusing namespace std;int T;int n,top;void in(int &x){    char ch=getchar();x=0;int flag=1;    while (!(ch>='0'&&ch<='9')) flag=ch=='-'?-1:flag,ch=getchar();    while (ch>='0'&&ch<='9')    x=x*10+ch-'0',ch=getchar();x*=flag;}struct QwQ{    int num,pos;    bool operator <(const QwQ &a)const    {        return num==a.num?pos<a.pos:num<a.num;    }}a[MAXN],b[MAXN];bool cmp(QwQ a,QwQ b){    return a.pos<b.pos;}int main(){    in(T);    while (T--)    {        top=0;in(n);        for (int i=1;i<=n;i++)  in(a[i].num),a[i].pos=i;        sort(a+1,a+n+1);        int last=-MAXINT;        for (int i=1;i<=n;i++)            if (a[i].num!=last) b[++top]=a[i],last=a[i].num;        sort(b+1,b+top+1,cmp);        for (int i=1;i<top;i++) printf("%d ",b[i].num);        if (top)    printf("%d\n",b[top].num);        else    printf("\n");    }}
0 0
原创粉丝点击