• [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
  • 相关阅读:
    福大软工1816 · 第一次作业
    Python学习
    实验12——指针的基础应用2
    实验11——指针的基础应用
    实验十——一维数组的定义及引用
    实验九——基本数据类型存储及应用总结
    实验八——函数定义及调用总结
    实验七——函数定义及调用总结
    实验六——循环结构程序练习总结
    实验五——循环结构学习总结
  • 原文地址:https://www.cnblogs.com/mrclr/p/9510084.html
Copyright © 2020-2023  润新知