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>
inline int read(){
	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[100000];
int main(){
	memset(sum,0,sizeof(sum));
	int n,x;
	int max=-1;
	int total=0;
	n=read();
	while(n--){
		x=read();
		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

adopt
 

Test pointresultMemorytime
Test point 1The answer is correct660KB3MS
Test point 2The answer is correct664KB5MS
Test point 3The answer is correct656KB5MS
Test point 4The answer is correct660KB2MS
Test point 5The answer is correct656KB3MS
Test point 6The answer is correct656KB3MS
Test point 7The answer is correct656KB3MS
Test point 8The answer is correct660KB4MS
Test point 9The answer is correct668KB3MS
Test point 10The answer is correct664KB3MS

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[32768];
int r[32768];
bool u[32768];
int ans[32768];
inline int read(){
	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)); 
	int x=read();
	for(int i=1;i<=x;i++){
		num[i]=read();
	}
	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;
}
 

The test results are as follows

Program running results


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

adopt
 

Test pointresultMemorytime
Test point 1The answer is correct316KB2MS
Test point 2The answer is correct304KB4MS
Test point 3The answer is correct312KB4MS
Test point 4The answer is correct312KB3MS
Test point 5The answer is correct316KB3MS
Test point 6The answer is correct312KB3MS
Test point 7The answer is correct312KB3MS
Test point 8The answer is correct316KB3MS
Test point 9The answer is correct320KB2MS
Test point 10The answer is correct316KB3MS

Working hard for dreams is worthy of youth!

Tags: Programming Algorithm

Posted on Sun, 17 Oct 2021 20:10:02 -0400 by siesmith