hdu 1157 Who's in the Middle (快排)

题目:

Problem Description
FJ is surveying his 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 < 10,000) 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.
 
Input
* Line 1: A single integer N 
* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
 Output
* Line 1: A single integer that is the median milk output.
 Sample Input
5 2 4 1 3 5
 Sample Output
3
 1 #include<stdio.h>
 2 
 3 int n;
 4 int  Partition(int A[],int p,int r)
 5 {
 6     int privot=A[p];
 7 
 8     int left=p;
 9     int right=r;
10 
11     while(right>left)
12     {
13         while(right>left&&A[right]>=privot)
14         {
15             right--;
16         }
17 
18         A[left]=A[right];
19 
20         while(right>left&&A[left]<=privot)
21         {
22             left++;
23         }
24 
25         A[right]=A[left];
26     }
27     A[left]=privot;
28     return left;
29 }
30 
31 void QuickSort(int A[],int p,int r)
32 {
33    int privot;
34 
35    if(r>p)
36    {
37        privot=Partition(A,p,r);
38        if(privot==n/2) return;
39        QuickSort(A,p,privot-1);
40        QuickSort(A,privot+1,r);
41    }
42 
43 }
44 
45 int main()
46 {
47     int A[10005];
48     while(~scanf("%d",&n))
49     {
50         for(int i=1;i<=n;i++)
51            scanf("%d",&A[i]);
52         QuickSort(A,1,n);
53         printf("%d\n",A[n/2+1]);
54     }
55 }
View Code

 

posted @ 2013-06-08 19:33  lysr__tlp  阅读(168)  评论(0编辑  收藏  举报