[Luo Gu] P1059 obviously random number

This is a great solution to a problem of violence Luo Gu P1059.

Subject description:

First, we see this problem going heavy method and sorted, so we can use the definition of violence in a bool array, each represents a positive integer if there have been, so the way to go to complete the heavy work. Finally, from small to large swept over the range of numbers to output. In this way, we use the huge space complexity in exchange for a $ O (n + k) $ complexity of time (for the maximum number of array input k). code show as below:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <stack>
#include <set>
#include <cstring>
#include <string>
#include <queue>
using namespace std;

#define file

inline void read(int &x){
    x=0;int f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    x*=f;
}

bool a[1005];
int digit[100005];
int ans=0;

void quick_sort(int *shuzu,int n)
{
    for(int i=1;i<=n;i++)
        if(!a[shuzu[i]])
        {
            a[shuzu[i]]=1;
            ans++;
        }
    cout<<ans<<endl;
    for(int i=1;i<=1000;i++)
        if(a[i])
            cout<<i<<" ";
}

int main(int argc, char const *argv[])
{
    #ifndef file
        char IN[105]=".in";
        char OUT[105]=".out";
        freopen(IN,"r",stdin);
        freopen(OUT,"w",stdout);
    #endif

    int n;
    read(n);
    for(int i=1;i<=n;i++)
        read(digit[i]);
    quick_sort(digit,n);

    #ifndef file
        fclose(stdin);
        fclose(stdout);
    #endif

    return 0;
}

Guess you like

Origin www.cnblogs.com/wjsoj/p/11566688.html