首页 > ACM题库 > HDU-杭电 > HDU 3194-Median[解题报告]HOJ
2014
03-06

HDU 3194-Median[解题报告]HOJ

Median

问题描述 :

This problem is also easy.
Given N numbers, and please find the Median of them. Median is the number which separates the higher half the numbers given from the lower half.

输入:

There are some cases. Process to the end of file.
Each case given as N A X0 B M separates by a single space, and you are going to generate the numbers as following:
  Xi = (Xi-1 * A + B) mod M
N would be less than 1000000 Both A, B, M and Xi would be a positive number less than 40000.

输出:

There are some cases. Process to the end of file.
Each case given as N A X0 B M separates by a single space, and you are going to generate the numbers as following:
  Xi = (Xi-1 * A + B) mod M
N would be less than 1000000 Both A, B, M and Xi would be a positive number less than 40000.

样例输入:

5 73 5 97 43

样例输出:

25

#include<cstdio>
#include<algorithm>
using namespace std;
#define N 1000

int a[N], n, i;
int main(){
    int m, ans;
    while( scanf("%d%d",&n,&m) != EOF){
        ans = 0;
        for(i=1; i<=n; i++)
            scanf("%d", a+i);
        sort(a+1, a+1+n);
        while(a[(n+1)/2] != m){
            a[++n] = m;
            sort(a+1, a+1+n);
            ans++;
        }
        printf("%d\n",ans);
    }

    return 0;
}

  1. int half(int *array,int len,int key)
    {
    int l=0,r=len;
    while(l<r)
    {
    int m=(l+r)>>1;
    if(key>array )l=m+1;
    else if(key<array )r=m;
    else return m;
    }
    return -1;
    }
    这种就能避免一些Bug
    l,m,r
    左边是l,m;右边就是m+1,r;

  2. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!

  3. Gucci New Fall Arrivals

    This is really nice to know. I hope it will be successful in the future. Good job on this and keep up the good work.