# 1184: explicit random number (one book pass) (comparison between merging and bucket sorting)

### 1184: explicit random number

Time limit: 1000 ms         Memory limit: 65536 KB
Number of submissions: 19979     Number of passes: 12535

### [Title Description]

Mingming wants to invite some students to do a questionnaire survey in the school. For the objectivity of the experiment, he first uses the computer to generate N random integers between 1 and 1000 (n ≤ 100). For the repeated numbers, only one is retained, and the other same numbers are removed. Different numbers correspond to different student numbers. Then sort these numbers from small to large, and go to the students to make a survey according to the arranged order. Please help Mingming to complete the work of "de duplication" and "sorting".

### [input]

There are 2 lines, the first line is a positive integer, indicating the number of generated random numbers: N;

Line 2 has N positive integers separated by spaces, which are the generated random numbers.

### [output]

It is also line 2. The first line is a positive integer M, which represents the number of different random numbers. The second line is M positive integers separated by spaces, which are different random numbers in good order from small to large.

### [input example]

```10
20 40 32 67 40 20 89 300 400 15```

### [output example]

```8
15 20 32 40 67 89 300 400```

The bucket code is as follows:

```#include<cstdio>
#include<cstring>
char c;int f=1;int res=0;
while(c<'0' || c>'9'){
if(c=='-')
f=-1;
c=getchar();
}
while(c>='0' && c<='9'){
res=(res<<1)+(res<<3)+(c^48);
c=getchar();
}
return f*res;
}
int sum;
int main(){
memset(sum,0,sizeof(sum));
int n,x;
int max=-1;
int total=0;
while(n--){
if(x>max)
max=x;
if(!sum[x]){
total++;
sum[x]++;
}
}
printf("%d\n",total);
for(int i=1;i<=max;i++)
if(sum[i])
printf("%d ",i);
return 0;
}```

The test results are as follows:

### Program running results

User name: Mary L, title number: 1184 , run No.: 12825237, code length: 567Bytes

 Test point result Memory time Test point 1 The answer is correct 660KB 3MS Test point 2 The answer is correct 664KB 5MS Test point 3 The answer is correct 656KB 5MS Test point 4 The answer is correct 660KB 2MS Test point 5 The answer is correct 656KB 3MS Test point 6 The answer is correct 656KB 3MS Test point 7 The answer is correct 656KB 3MS Test point 8 The answer is correct 660KB 4MS Test point 9 The answer is correct 668KB 3MS Test point 10 The answer is correct 664KB 3MS

The code for merging and sorting is as follows. It is a little redundant. Optimize it yourself

```#Include < cstdio > / / merge sort
#include<cstring>
int num;
int r;
bool u;
int ans;
char c;int f=1;int res=0;
while(c<'0' || c>'9'){
if(c=='-')
f=-1;
c=getchar();
}
while(c>='0' && c<='9'){
res=(res<<1)+(res<<3)+(c^48);
c=getchar();
}
return f*res;
}
void msort(int left,int right){
if(left==right)return;
else{
int mid=(left+right)/2;
msort(left,mid);
msort(mid+1,right);
int i=left;
int j=mid+1;
int k=left;
while(i<=mid && j<=right){
if(num[i]>=num[j]){
r[k]=num[j];
k++;
j++;
}
else if(num[i]<num[j]){
r[k]=num[i];
k++;
i++;
}
}
while(i<=mid){
r[k]=num[i];
k++;
i++;
}
while(j<=right){
r[k]=num[j];
j++;
k++;
}
for(int i=left;i<=right;i++)
num[i]=r[i];
}
}
int main(){
memset(u,true,sizeof(u));
for(int i=1;i<=x;i++){
}
msort(1,x);
int k=0;
for(int i=1;i<=x;i++){
if(u[num[i]]){
k++;
ans[k]=num[i];
u[num[i]]=false;
}
}
printf("%d\n",k);
for(int i=1;i<=k;i++){
printf("%d ",ans[i]);
}
return 0;
}
```

### Program running results

User name: Mary L, title number: 1184 , run No.: 12825253, code length: 1233Bytes