C - A mid-summer night's dream. (中位数的性质)->UVA - 10057

Problem C

A mid-summer night’s dream

Input: standard input

Output: standard output

This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because this problem is being sent back to 2000AD with the help of time machine. Now it is possible to establish direct connection between man and computer CPU. People can watch other people’s dream on 3D displayer (That is the monitor today) as if they were watching a movie. One problem in this century is that people have become so dependent on computers that their analytical ability is approaching zero. Computers can now read problems and solve them automatically. But they can solve only difficult problems. There are no easy problems now. Our chief scientist is in great trouble as he has forgotten the number of his combination lock. For security reasons computers today cannot solve combination lock related problems. In a mid-summer night the scientist has a dream where he sees a lot of unsigned integer numbers flying around. He records them with the help of his computer, Then he has a clue that if the numbers are (X1, X2,   …  , Xn) he will have to find an integer number A (This A is the combination lock code) such that

             

             (|X1-A| + |X2-A| + … … + |Xn-A|) is minimum.

Input

Input will contain several blocks. Each block will start with a number n (0<n<=1000000) indicating how many numbers he saw in the dream. Next there will be n numbers. All the numbers will be less than 65536. The input will be terminated by end of file.

Output

For each set of input there will be one line of output. That line will contain the minimum possible value for A. Next it will contain how many numbers are there in the input that satisfy the property of A (The summation of absolute deviation from A is minimum). And finally you have to print how many possible different integer values are there for A (these values need not be present in the input). These numbers will be separated by single space.

Sample Input:

2
10
10
4
1
2
2
4

Sample Output:

10 2 1
2 2 1

题意:我们要找1个数使得 (|X1-A| + |X2-A| + … … + |Xn-A|) is minimum.这便是中位数的功劳了。如果n是奇数好办,就是排完序后中间那个,次数为在a中的出现次数,为方便可用map映射,sum为符合条件的不同值的个数,此时为1。

如果n为偶数,那么最小值就是中间两个的左边那个,出现次数为中间两个数区间的值在a数组的出现次数,sum为符合条件的不同值的个数。即r-l+1.

#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
map<int,int>mp;
int a[1000010],n,num,cnt,sum; //num为符合条件的最小值,cnt为符合条件的数在a数组的出现次数,sum为符合条件的个数
int main()
{
    while(cin>>n)
    {
        mp.clear();
        num=cnt=sum=0;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            mp[a[i]]++;  //为了计算方便而进行映射,也可不映射而暴力也能过
        }
        sort(a+1,a+1+n);
        if(n%2==1)
        {
            num=a[n/2+1];   //中位数
            cnt=mp[a[n/2+1]];//出现次数
            sum=1;          //符合条件的个数,由于为奇数,为1
        }
        else
        {
            num=a[n/2];  //最小符合条件的值
            cnt=0;
            int l=a[n/2];   //由于n为偶数,则中间2个区间内的所有数全部符合题意,因为每移动1位,前面与后面的差距是同步的
            int r=a[n/2+1];
            for(int i=l;i<=r;i++)//符合条件的值在a数组中的出现次数
                cnt+=mp[i];
            sum=r-l+1;      //符合条件的值的个数
        }
        cout<<num<<" "<<cnt<<" "<<sum<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43824158/article/details/88218438