[POJ] 2388 -> Who's in the Middle
来源:互联网 发布:十日惊奇 知乎 编辑:程序博客网 时间:2024/06/07 10:33
Who's in the Middle
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 33719 Accepted: 19632
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.
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.
* 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
524135
Sample Output
3
水题
Code:
#include <iostream>#include <algorithm>using namespace std;int main(){int n;cin >> n;int* arr = new int[n];for (int i = 0; i < n; i++)cin >> arr[i];sort(arr, arr + n);cout << arr[n / 2] << endl;return 0;}
期待为线性时间的选择算法求解
#include <iostream> using namespace std;int partion(int* arr, int left, int right){int i = left, j = right, x = arr[i];while (i < j){while (i < j && x <= arr[j]) --j;if (i < j) arr[i++] = arr[j];while (i < j && x > arr[i]) ++i;if (i < j) arr[j--] = arr[i];}arr[i] = x;return i;}int select(int*arr, int left, int right, int index){int n = partion(arr, left, right);if (n == index) return arr[n];if (index < n) select(arr, left, n - 1, index);else select(arr, n + 1, right, index);}int main(){int n;scanf_s("%d", &n);int* arr = new int[n];for (int i = 0; i < n; i++)scanf_s("%d", &arr[i]);printf("%d\n", select(arr, 0, n - 1, n / 2));return 0;}
0 0
- POJ--2388 Who's in the Middle
- poj 2388 Who's in the Middle
- POJ-2388 who's in the middle
- poj-2388 Who's in the Middle
- poj 2388 Who's in the Middle
- POJ 2388 Who's in the Middle
- poj 2388 Who's in the Middle
- POJ 2388 Who's in the Middle
- Poj 2388 Who's in the Middle
- POJ 2388 Who's in the Middle
- POJ 2388 Who's in the Middle
- poj 2388 Who's in the Middle
- POJ-2388-Who's in the Middle
- poj 2388 Who's in the Middle
- POJ 2388 Who's in the Middle
- poj 2388 Who's in the Middle
- Who's in the Middle(poj 2388)
- POJ 2388:Who's in the Middle
- 从VS2010中复制带有中文字符的代码到Office等时,出现乱码的解决方案
- 找工作小总结
- 如何为Apache JMeter开发插件(一)
- MyEclipse设置Java代码注释模板
- Ubuntu下安装配置tftp服务器
- [POJ] 2388 -> Who's in the Middle
- 数据结构 - 二叉树的遍历
- C语言中memset函数详解
- 内存映射文件原理探索
- Linux grep命令
- Facebook 的库Fresco在ecplise中的使用
- 火狐flash崩溃
- OpenCV Viz 3D虚拟空间模块
- 【转】碰到MySQL无法启动1067错误问题解决方法