POJ 2388:Who's in the Middle:快速排序思想求解中位数
2014-09-20 01:22
330 查看
Who's in the Middle
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.
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.
Output
* Line 1: A single integer that is the median milk output.
Sample Input
Sample Output
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
USACO 2004 November
快排都不能在不调试的情况下写完整~~
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 31977 | Accepted: 18617 |
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.
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.
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
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; else if(lef<order) findMiddle(lef+1,y,order); else findMiddle(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; }
相关文章推荐
- POJ 2388 Who's in the Middle(排序)
- POJ 2388 Who's in the Middle(排序)
- HDU 1157 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
- HDU 1.3.8 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
- HDU 1157 Who's in the Middle (快速排序 or 任意排序)
- POJ--2388 Who's in the Middle
- poj 2388 Who's in the Middle
- POJ 2388 Who's in the Middle 快排解法