HDU --1157 Who's in the Middle——nth_element

3 篇文章 0 订阅
1 篇文章 0 订阅

 

最快求无序数列中第几个元素——Who's in the Middle——nth_element


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

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.

 

题意::给出无序的一组数(奇数个),求中间的数。

思路1::排好序,输出中间的数,可以用各种排序方法;

思路2::利用快排的思想,只对包含中间元素的一半进行递归运算;

思路3::利用库函数nth_element直接进行求解。

三种方法不言而喻,最后一种代码最简单,他的原理跟思路二类似,时间复杂度相同。

第一种思路过于简单此处不贴代码,可以用sort函数;

第二种思路代码:: 


#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3F3F3F3F
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
//const int MAXN = 10010;
//#define INF 0x3FFFFFFF
/*************************************
 **************头文件*****************
 *************************************/
 
int a[500005],b[500005];
int n;

int QuickSort(int a[],int low,int high)
{
	int  e = a[low];
	int i = low,j = high;
	while(i<j)
	{
		while(i<j&&a[j]>=e) j--;
		if(i<j)
		{
			a[i++] = a[j];
		}
		while(i<j&&a[i]<=e) i++;
		if(i<j)
		{
			a[j--] = a[i];
		}
	}
	a[i] = e;
	//当(n-1)/2<i时中间数在[low,i-1]之间
	if((n-1)/2<i&&low<i-1) QuickSort(a,low,i-1);
	//当(n-1)/2>i时中间数在[i+1,high]之间
	if((n-1)/2>i&&i+1<high)QuickSort(a,i+1,high);
	//其他的情况可以不做处理
}


int main()
{
	
	while(cin>>n)
	{
		REP(i,n)
		{
			cin>>a[i];
		}
		QuickSort(a,0,n-1);
		cout<<a[(n-1)/2];
	} 
	return 0;
} 
 
 
 


 下面是第三种思路解答,十分简洁::

 

 

#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3F3F3F3F
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
//const int MAXN = 10010;
//#define INF 0x3FFFFFFF
/*************************************
 **************头文件*****************
 *************************************/
 
int a[50005];
int main()
{
	int n;
	while(cin>>n){
		REP(i,n)
			cin>>a[i];
		//三个参数分别表示起始地址,所求的第几个数地址,终止地址[)左闭右开
		nth_element(a,a+(n-1)/2,a+n);
		cout<<a[(n-1)/2];
	} 
	return 0;
} 
 
 
 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值