poj 2388 Who's in the Middle

Who's in the Middle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 29593 Accepted: 17166

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. 
FJ在调查自己的牛群来寻找最平均的奶牛。 他想 知道这只 “中等”牛能产多少奶 :一半的牛产奶比它多,一半的牛产奶比它少
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.
给出一个奇数N (1 <= N < 10,000)作为牛的头数,并给出他们的产奶量 (1..1,000,000),找出那头产奶中等的牛。

Input

* Line 1: A single integer N 
一个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

Hint

INPUT DETAILS: 

Five cows with milk outputs of 1..5 

OUTPUT DETAILS: 

1 and 2 are below 3; 4 and 5 are above 3.

Source


额,这道题就是排序一下求中值,wa了两次。。。一次是眼花写错了,一次是输出格式忘改了。。。所以没啥好注意的。。。
用的堆排序,多种排序都可以。快排也行,刚好学写堆,这道题过后可以盲敲堆排了,看来多敲几次确实有利于理解。。。
#include <stdio.h>
#define PARENT(i) (i >> 1)
#define LEFT(i) (i << 1)
#define RIGHT(i) (i << 1) + 1
#define N 10001
int heapsize;
void MAX_HEAPIFY(int a[N], int i){
	int l = LEFT(i);
	int r = RIGHT(i);
	int largest, temp;
	if (l <= heapsize && a[l] > a[i]){
		largest = l;
	}else{
		largest = i;
	}
	if (r <= heapsize && a[r] > a[largest]){
		largest = r;
	}
	if (largest != i){
		temp = a[i]; a[i] = a[largest]; a[largest] = temp;
		MAX_HEAPIFY(a, largest);
	}
}

void BUILD_MAX_HEAP(int a[N], int len){
	int i;
	heapsize = len;
	for (i = (heapsize >> 1); i >= 1;i --){
		MAX_HEAPIFY(a, i);
	}

}

void HEAPSORT(int a[N], int len){
	int i, temp;
	BUILD_MAX_HEAP(a, len);
	for (i = len; i >= 2;i --){
		temp = a[1]; a[1] = a[i];a[i] = temp;
		heapsize--;
		MAX_HEAPIFY(a, 1);
	}

}
int main(void){
	int n;
	int a[N];
	int i;
	//freopen("in", "r", stdin);
	while(scanf("%d", &n) != EOF){
		for (i = 1;i <= n;i ++){
			scanf("%d", &a[i]);
		}
		HEAPSORT(a, n);
//		for (i = 1;i <= n;i ++)
//            printf("%d ", a[i]);
		printf("%d\n", a[n / 2 + 1]);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值