1753: [Usaco2005 qua]who ' s in the middle time limit:5 Sec Memory limit:64 MB
submit:290 solved:242
[Submit] [Status] [Discuss] DESCRIPTIONFJ is surveying he herd to find the most average cow. He wants to know how much milk this ' median ' cow gives:half of the cows give as much or more than the median; Half give as much or less. Given an odd number of cows n (1 <= n <) and their milk output (1..1,000,000), find the median amount of milk Given such that at least half the cows give the same amount of milk or more and at least half give the same or less. Enter the number of N and output the middle number in ascending order. input* Line 1: A single integer N * Lines 2..n+1:each line contains A single integer so is the milk output of one COW.O utput* Line 1: A single integer which is the median milk output. Sample Input5
2
4
1
3
5
INPUT DETAILS:
Five cows with milk outputs of 1..5
Sample Output3
OUTPUT DETAILS:
1 and 2 are below 3; 4 and 5 are above 3.
HINT Source
Gold
Puzzle: It is difficult to imagine such a topic is actually USACO gold group = =, Khan.
In fact, the direct sort is good, but this is too boring, so a simple small optimization (the program is as follows, very clear, I believe you must understand)
1/**************************************************************2Problem:17533 User:hansbug4 language:pascal5 result:accepted6Time: AMs7Memory:616KB8****************************************************************/9 Ten var One I,j,k,l,m,n,x,y:longint; AA:Array[0..100000] ofLongint; - proceduresort (l,r:longint); - varI,j,x,y:longint; the begin - if(l>m)or(r<m) ThenExit //Here !!! ^_^ -i:=l;j:=r;x:=a[(L+r)Div 2]; - Repeat + whileA[i]<x DoInc (i); - whileA[j]>x DoDec (j); + ifI<=j Then A begin aty:=a[i];a[i]:=a[j];a[j]:=y; - Inc (I);d EC (j); - End; - untilI>J; - ifI<r Thensort (i,r); - ifL<j Thensort (l,j); in End; - to begin +READLN (n); m:= (1+n)Div 2; - fori:=1 toN Doreadln (A[i]); theSort1, n); * Writeln (a[m]); $ Readln;Panax Notoginseng End.
1753: [Usaco2005 qua]who ' s in the middle