Topic Links: point me to jump ~
Test instructions: To find the median number.
Precautions: (1) n maximum is 250000. (2) Memory limit size is 1500KB.
Idea: Due to memory limitations, only an array of approximately N/2 size can be opened. If n is even, after sorting, the position of the median will appear in N/2, and N/2 + 1,
If n is an odd number then the median position is N/2 + 1, so the array size opens N/2 + 1. First, create a large heap of N/2 + 1 numbers. For the later
Enter the maintenance of this big root heap. After the input is complete, the number of the largest in the heap, or the first second largest, is calculated according to the parity of N.
If the pile is sorted before, the problem will be almost AC.
Heap Sort
To the neutralization, the Heaven Status Yan, all things. Brother Qiushi is a man who pursues the golden mean.
Although Qiushi elder brother's admirers many, but Qiushi elder brother does not like the extreme sister paper. So he wanted to pick a middle-of-the-line from all the sister papers that admired him.
Every sister paper to Qiushi elder brother's admiration degree can be expressed by an integer AI, brother Qiushi want to find out the median of these numbers.
The method for calculating the median of a finite number of data is:
Sort all the same data in the order of size. If the number of data is odd, then the middle data is the median of this group of data, and if the number of data is even, then the arithmetic mean of the 2 data is the median of this group of data.
Input
The first line has an integer n, which indicates the number of admirers of Qiushi.
Next n rows, each line has a positive integer AI.
1≤n≤250000, 1≤a i <2 ^31.
Output
Outputs the median of this n n number, preserving one decimal place.
Sample Input and output
3123
2.0
Hint
Note the memory size limit.
Code:
#include <stdio.h>#include<algorithm>#defineN 125007using namespacestd;voidDownadjust (int*num,intIintN//find the right position for the I element down{ intLchild, Rchild; Lchild= i *2; Rchild= i *2+1; intMax_i =i; if(I <= n/2) { if(Num[lchild] > num[max_i] && lchild <=N) max_i=Lchild; if(Num[rchild] > num[max_i] && rchild <=N) max_i=Rchild; if(Max_i! =i) {swap (Num[i], num[max_i]); Downadjust (num, max_i, n); } }}voidBuild (int*num,intN//build a big root heap{ for(inti = n/2; I >=1; i--) downadjust (num, I, n);}voidSort (int*num,intN//Heap Sort{Build (num, n); for(inti = n; I >=1; i--) {swap (num[i], num[1]); Downadjust (num,1I1); }}intMain () {intN, Num[n]; while(SCANF ("%d", &n)! =EOF) { for(inti =1; I <= n/2+1; i++) {scanf ("%d", &Num[i]); } Build (num, n/2+1); inttemp; for(inti = n/2+2; I <= N; i++)//for the latter half of the input maintenance Dagen{scanf ("%d", &temp); if(Temp >= num[1]) Continue; num[1] =temp; Downadjust (num,1, n/2+1); } Sort (num, n/2+1); if(n%2) {printf ("%.1lf\n", (Double) num[n/2+1]); } Else { DoubleAns = num[n/2] >= num[n/2-1] ? num[n/2]: num[n/2-1]; Ans+= (Double) num[n/2+1]; Ans/=2.0; printf ("%.1lf\n", ans); } } return 0;}
UESTC 1063 Qiushi Big Brother and sister (two fork heap)