F-Qiushi brother and sister paper Time limit:3000/1000ms (java/others) Memory limit:1500/1500kb (java/others)SubmitStatus
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 , Qiushi want to find out the median number 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 row has a positive integer a i.
1≤n≤250000 , 1≤ai<2.
Output
Outputs the median of this n number, preserving one decimal place.
Sample Input and output
Sample Input |
Sample Output |
3123 |
2.0 |
Hint
Note the memory size limit.
Problem Solving Report:
Memory can only be half-open. We use a large heap to maintain, the first half directly read into the large heap, for the second half of the data, if the number is greater than the heel of Max, then the element is useless, if less than, pop out of Max value, and then press the value into the heap, and finally according to the parity of N to read the first two layers of the heap.
Because the memory limit is very tight. So use the C language to write
#include <stdio.h>#defineMAXN 125005intHeapSize =0, Heap[maxn],maxheapsize,n;voidSwapint*x,int*y) { inttemp = *x; *x = *y; *y =temp;}voidInsertintx) {heap[++heapsize] =x; intCur = heapsize, pre = heapsize/2; while(Pre >0) { if(Heap[cur] >Heap[pre]) {Swap (&heap[cur],&Heap[pre]); Cur=Pre; Pre= cur/2; } Else Break; }}voidmaintainheap () {//New Ele insert,maintain the heap intCur =1, tar =2*cur; while(Tar <=maxheapsize) { if(Tar +1<= heapsize && heap[tar+1] >Heap[tar]) tar++; if(Heap[tar] >Heap[cur]) {Swap (&heap[tar],&Heap[cur]); Cur=tar; Tar= cur*2; } Else Break; }}intMainintargcChar*argv[]) { inti; scanf ("%d",&N); Maxheapsize= n/2+1; for(i =1; I <= maxheapsize; ++i) {inttemp; scanf ("%d",&temp); Insert (temp); } for(; I <= n; + +)i) {inttemp; scanf ("%d",&temp); if(Temp < heap[1]) {heap[1] =temp; Maintainheap (); } } Long LongAns =0; if(n%2) {ans= heap[1]; printf ("%.1f\n",(Double) ans); } Else{ans= heap[1]; Long LongQans = heap[2]; if(heap[3] >Qans) Qans= heap[3]; Ans+=Qans; printf ("%.1f\n",(Double) ans/2.*1.); } return 0;}
Uestc_ Qiushi brother and sister paper UESTC Training for Data Structures<problem f>