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

来源:互联网 发布:婴儿被子品牌知乎 编辑:程序博客网 时间:2024/06/05 11:03

 

最快求无序数列中第几个元素——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

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.

 

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

思路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 iteratortypedef 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 iteratortypedef 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
原创粉丝点击