/*
Who's in the Middle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 41845 Accepted: 24201
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
题意;输入n个数,找出一个数,满足至少有一半大于或等于它,并且有一半小于或等于它。
对挖坑填数进行总结
1.i =L; j = R; 将基准数挖出形成第一个坑a[i]。
2.j--由后向前找比它小的数,找到后挖出此数填前一个坑a[i]中。
3.i++由前向后找比它大的数,找到后也挖出此数填到前一个坑a[j]中。
4.再重复执行2,3二步,直到i==j,将基准数填入a[i]中
Who's in the Middle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 41845 Accepted: 24201
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
题意;输入n个数,找出一个数,满足至少有一半大于或等于它,并且有一半小于或等于它。
对挖坑填数进行总结
1.i =L; j = R; 将基准数挖出形成第一个坑a[i]。
2.j--由后向前找比它小的数,找到后挖出此数填前一个坑a[i]中。
3.i++由前向后找比它大的数,找到后也挖出此数填到前一个坑a[j]中。
4.再重复执行2,3二步,直到i==j,将基准数填入a[i]中
*/
#include <cstdio>
#include <cstring>
using namespace std;
const int Max = 10000+10;
void quick_sort(int s[],int l,int r)
{
if(l<r)
{
int i = l,j = r, x = s[l];// 即s[l]就是第一个坑
while(i<j)
{
while(i<j && s[j] >= x)// 从右向左找小于x的数来填s[i]
j--;
if(i < j)
s[i++] = s[j];//将s[j]填到s[i]中,s[j]就形成了一个新的坑
while(i<j && s[i] < x)// 从左向右找大于或等于x的数来填s[j]
i++;
if(i<j)
s[j--] = s[i];//将s[i]填到s[j]中,s[i]就形成了一个新的坑
}
s[i] = x;//退出时,i等于j。将x填到这个坑中
quick_sort(s,l,i-1);
quick_sort(s,i+1,r);
}
}
int main()
{
int n;
int i;
int s[Max];
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d",&s[i]);
}
quick_sort(s,0,n-1);
printf("%d\n",s[n/2]);
}
return 0;
}
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 10000+10;
int s[MAX];
int size;
void sift(int i)
{
int l, r, largest;
l = i*2; //左孩子
r = i*2+1; //右孩子
if(l <= size && s[l] > s[i]) { //左孩子 的值给根的值 比较
largest = l;
}
else {
largest = i;
}
if(r <= size && s[r] > s[largest]) { //右孩子 跟 上面找出的最大的那个比较
largest = r;
}
if(i != largest) { //如果之前的 根节点 给找到那个最大的 节点的值不一致 ,交换位置
int t = s[i];
s[i] = s[largest];
s[largest] = t;
sift(largest);
}
}
void create_heap()//初建堆
{
int i;
for(i=size/2;i>=1;i--){
sift(i);
}
}
void HeapSort()//堆排
{
int i;
create_heap();
for(i=size;i>=2;i--) //所有的节点值都给根节点交换位置
{
int t = s[1];
s[1] = s[i];
s[i] = t;
size -- ;
sift(1);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i;
for(i=1;i<=n;i++){
scanf("%d",&s[i]);
}
size = n;
HeapSort();
// for(i=1;i<=n;i++){
// printf("%d ",s[i]);
// }
printf("%d\n",s[n/2+1]);
}
return 0;
}