HDU 1157 Who's in the Middle (快速排序)
2015-08-17 15:43
239 查看
Problem 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
5
2
4
1
3
5
Sample Output
3
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
#include<stdio.h> void sort(int *a,int l,int r) { int x=a[l]; //首先记下一个基准数 int i=l,j=r; if(l>=r) return ; while(i<j) { while(i<j&&a[j]>=x) // 从右向左找第一个小于x的数 j--; a[i]=a[j]; while(i<j&&a[i]<=x) // 从左向右找第一个大于等于x的数 i++; a[j]=a[i]; } a[i]=x; sort(a,l,i-1); //递归调用对左半段排序 sort(a,i+1,r); //递归调用对右半段排序 } int main() { int i,n,a[10001]; while(~scanf("%d",&n)) { for(i=0;i<n;i++) scanf("%d",&a[i]); sort(a,0,n-1); printf("%d\n",a[n/2]); } return 0; }
相关文章推荐
- Android : View.getWidth() 和 view.getHeight() 返回0
- Android WebView的cookie第一次加载不工作的问题的解决
- 单例设计模式
- 纯代码利用CSS3 圆角边框和盒子阴影 制作 iphone 手机效果
- innodb master主线程
- 请不要用SECONDS_BEHIND_MASTER来衡量MYSQL主备的延迟时间
- SQLite 创建表
- AltiumDesigner99——常用快捷键
- Leetcode#57||Insert Interval
- @property中的属性关键字整理
- [leetcode] 218.The Skyline Problem
- 自定义控制器转场动画及实现下拉菜单的小Demo
- windows下oracle自动备份
- 阻尼滑动--可以滑动过度的ScrollView(OverScrollView)
- Delphi开发学习一:使用ADOConnection控件连接数据库SQL
- java简易图书管理之三层模式
- fir.im Weekly - 工欲善其事,必先利其器
- Jquery-DataTable 使用介绍
- 为OCR和Votingdisk准备rawdevices
- 如果让我重新设计一款Android App