POJ_2388 Who's in the Middle
来源:互联网 发布:http借口怎么写java 编辑:程序博客网 时间:2024/05/23 18:12
Who's in the Middle
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 26734 Accepted: 15432
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
题意:一堆数中找中位数
思路:顺序统计(找第K大数)
代码如下:
#include<iostream>#define SIZE 10005using namespace std;int num[SIZE];void swap(int &a, int &b){int tmp;tmp = a;a = b;b = tmp;}int partition(int p, int r){int i,j,q;i = p - 1;q = num[r];for(j = p; j <= r - 1; j++){if(num[j] < q){i++;swap(num[i],num[j]);}}i++;swap(num[i],num[r]);return i;}int select(int p, int r, int i){int q,k;if(p == r)return num[p];q = partition(p, r);k = q - p + 1;if(i == k)return num[q];else if(i < k)return select(p,q - 1, i);elsereturn select(q + 1, r, i - k);}int main(){int i,n,p;cin >> n;for(i = 1; i <= n; i++){cin >> num[i];}p = select(1, n, n / 2 + 1);cout << p << endl;return 0;}
- POJ_2388 Who's in the Middle
- poj_2388 Who's in the Middle(排序)
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- 深度分析NandFlash—控制器参数TACLS、TWRPH0和TWRPH1的确定(以TQ2440开发板上的K9F2G08U0A为例)
- php面试题之简答题002
- Android IPC进程间通讯机制
- WIN8 系统 按下ctrl按键直接关闭文件夹,结束explorer进程解决方案
- Mvvm Light Toolkit for wpf/silverlight系列之Messenger
- POJ_2388 Who's in the Middle
- AC 经典多模式匹配算法
- Coreseek $sphinxapi设置实例和$result结构分析
- Android 只开启一个Activity实例
- php面试题之问答题 003
- IT旅途——程序员面试经验分享(作者季红)
- 合作开发之后谈设计模式
- PTT BBS
- 百度地图之3D图层