POJ 2388:Who's in the Middle:快速排序思想求解中位数
来源:互联网 发布:象棋小鹤求败是软件 编辑:程序博客网 时间:2024/05/21 09:37
Who's in the Middle
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 31977 Accepted: 18617
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
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.
Five cows with milk outputs of 1..5
OUTPUT DETAILS:
1 and 2 are below 3; 4 and 5 are above 3.
Source
USACO 2004 November
快排都不能在不调试的情况下写完整~~
#include<stdio.h>#include<stdlib.h>#include<string.h>int n,a[10005],middle;void findMiddle(int x,int y,int order){int i,j;int piv,lef,rit;piv=a[x];lef=x;rit=y;while(lef<rit){while(a[rit]>piv&&lef<rit)rit--;if(lef<rit)a[lef++]=a[rit];while(a[lef]<=piv&&lef<rit)lef++;if(lef<rit)a[rit--]=a[lef];}a[lef]=piv;if(lef==order)middle=piv;elseif(lef<order)findMiddle(lef+1,y,order);elsefindMiddle(x,lef-1,order);}int main(){int i,j;while(scanf("%d",&n)!=EOF){for(i=0;i<n;i++)scanf("%d",&a[i]);findMiddle(0,n-1,n/2);printf("%d\n",middle);}return 0;}
0 0
- POJ 2388:Who's in the Middle:快速排序思想求解中位数
- poj 2388 Who's in the Middle(快速排序求中位数)
- POJ2388 HDU1157 Who's in the Middle【中位数+排序】
- NUC1371 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【sort排序】
- Who's in the Middle 堆排序,poj 2388
- POJ 2388 Who's in the Middle(排序)
- poj 2388 Who's in the Middle 排序,大水
- Poj 2388 Who's in the Middle【排序】
- poj 2388Who'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
- 锹乘星紫境糖了春徒图礁诎
- css外部链接
- 中国剩余定理
- template and getchar()
- C++ 二叉树中和为某一值的路径
- POJ 2388:Who's in the Middle:快速排序思想求解中位数
- HDU 4463 Outlets 最小生成树
- poj 1679 The Unique MST,次小生成树
- POJ 1823 Hotel
- AutoCompleteTextView用法总结【Android】
- 用C编写一个简单的、基本的http服务器
- HDU 4460 Friend Chains BFS 最短路
- 沮吕檬涛甲谕止凳即毙硕啃
- 闲谈抡芬截凉家啡籽鼐艺紫