Sorttime limit: 6000/1000 ms (Java/other) memory limit: 65536/32768 K (Java/other) total submission (s): 15 accepted submission (s): 9 Font: times New Roman | verdana | georgiafont size: Numbers → Problem description give you n integers. output the numbers of the first m in ascending order. There are two rows of test data in each input group. The first row has two numbers n, m (0 <n, m <1000000), and the second row contains n different numbers, and all are integers in the range. Output outputs the first m of each group of test data in ascending order. Sample Input
5 33 -35 92 213 -644
Sample output
213 92 3
A simple sorting question, but due to the large data volume, it is a little difficult to quickly sort the AC time by 937 Ms. The bubble estimation is definitely not good. The code for the fast sorting is as follows:
#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#include<time.h>int data[1000000];int N,M;int cmp(const void *a,const void *b){ return *(int *)b-*(int *)a;}int main(){ while(~scanf("%d%d",&N,&M)) { for(int i=0;i<N;++i) scanf("%d",&data[i]); qsort(data,N,sizeof(int),cmp); for(int i=0;i<M;++i) { printf("%d",data[i]); if(i<M-1) printf(" "); } puts(""); } return 0;}
The next step is the priority queue, which has not been written for a while. The AC time is 750 ms. The Code is as follows:
# Include <stdio. h> # include <stdlib. h> # include <math. h> # include <string. h> # include <time. h> # include <conio. h> int queue [1000005], CN; int M, N; void swap (int x, int y) {int T = queue [x]; queue [x] = queue [y]; queue [y] = T;} void up (int x) {If (x> 1) {If (queue [x]> queue [X/2]) {swap (X, X/2); up (X/2 );}}} void down (int x) {// sink small elements to int P = 2 * X; If (P <= cn) {If (p + 1 <= cn & queue [p] <Queue [p + 1]) {++ P;} If (queue [x] <queue [p]) {swap (x, P ); down (p) ;}} void POP () {queue [1] = queue [CN --]; down (1);} void insert (int x) {queue [++ cn] = x; up (CN) ;}int main () {While (~ Scanf ("% d", & N, & M) {Cn = 0; while (n --) {int C; scanf ("% d ", & C); insert (c) ;}for (INT I = 1; I <= m; ++ I) {printf ("% d", queue [1]); pop (); if (I <m) {printf ("") ;}} puts ("");} return 0 ;}
Finally, the hash thought is done. In the question, the data is-500000 to 500000, so 500000 is added to each data. In this way, a 1000005 array can correspond to each number. After the final traversal, we can find the large numbers of the first M.
The Code is as follows:
#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#include<time.h>int num[1000005];int hash( int x ){ return x+ 500000;}int back( int x ){ return x- 500000;}int main( ){ int N, M, cnt, min, max; while( ~scanf( "%d%d", &N, &M ) ) { cnt= 0; min= 0x7fffffff; max= 0x7fffffff+ 1; memset( num, 0, sizeof( num ) ); while( N-- ) { int c; scanf( "%d", &c ); if( min> hash( c ) ) { min= hash( c ); } if( max< hash( c ) ) { max= hash( c ); } num[ hash( c ) ]= 1; } for( int i= max; i>= min; --i ) { if( num[i] ) { cnt++; printf( "%d", back( i ) ); if( cnt< M ) { printf( " " ); } if( cnt== M ) { break; } } } puts( "" ); }}