时间限制: 1000ms 内存限制: 65535KB
通过次数: 1总提交次数: 1
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.
* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
5 2 4 1 3 5
3
Five cows with milk outputs of 1..5
OUTPUT DETAILS:
1 and 2 are below 3; 4 and 5 are above 3.
问题分析:(略)
这个问题和《POJ2388 HDU1157 Who's in the Middle【中位数+排序】》是同一个问题,代码直接用就AC了。
程序说明:参见参考链接。
参考链接:POJ2388 HDU1157 Who's in the Middle【中位数+排序】
题记:程序做多了,不定哪天遇见似曾相识的。AC的C++程序如下:
/* POJ2388 Who's in the Middle */ #include <iostream> #include <algorithm> using namespace std; const int N = 10000; int a[N]; int main() { int n; // 输入数据 while(cin >> n) { for(int i=0; i<n; i++) cin >> a[i]; // 排序 sort(a, a+n); // 输出结果 cout << a[n / 2] << endl; } return 0; }