B2761 [JLOI2011]不重复数字 离散化

就是一道离散化的裸题,但是在写的时候遇到了一些不可描述的问题,但是还是很顺利的。

题干:

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来进行输入输出操作,以免浪费不必要的时间。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
struct node
{
    ll v,id;
    bool operator == (const node other) const
    {
        return v == other.v;
    }
    bool operator < (const node other) const
    {
        if(v != other.v)
        return v < other.v;
        else
        return id < other.id;
    }
}a[50010];
int n;
bool cmp(node a,node b)
{
    return a.id < b.id;
} 
int main()
{
    int t;
    read(t);
    while(t--)
    {
        read(n);
        duke(i,1,n)
        {
            read(a[i].v);
            a[i].id = i;
        } 
        sort(a + 1,a + n + 1);
        int k = unique(a + 1,a + n + 1) - a - 1;
        sort(a + 1,a + k + 1,cmp);
        duke(i,1,k) 
        printf("%lld ",a[i].v);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/DukeLv/p/9693881.html
今日推荐