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

题意:找出一个A使得(|X1-A| + |X2-A| + … … + |Xn-A|)是最小的。。然后要输出的3个数字为:最小的A。满足条件的A在序列中出现的几次。满足条件的A的个数。。

思路:这题目其实就是转换成求中位数的问题。一个序列的中位数,和序列中每一个数的差的绝对值之和肯定是最小的。。 不过在这题中。由于A一定是整数。。所以情况可能不止1种。要注意。。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n, i;
int Conut;
int ge;
int sb[1000005];

int main() {
	while (~scanf("%d", &n)) {
		ge = 0;
		memset(sb, 0, sizeof(sb));
		for (i = 0; i < n; i ++)
			scanf("%d", &sb[i]);
		sort(sb, sb + n);
		if (n % 2) {
			Conut = - 1;
			ge = 1;
			int sbb = sb[(n - 1)/ 2];
			for (i = (n - 1) / 2; sb[i] == sbb && i < n; i ++)
				Conut ++;
			for (i = (n - 1) / 2; sb[i] == sbb && i >= 0; i --)
				Conut ++;
			printf("%d %d %d\n", sbb, Conut, ge);
		}
		else {
			int sbb1 = sb[n / 2 - 1];
			int sbb2 = sb[n / 2];
			if (sbb1 != sbb2) {
				ge = 2 + sbb2 - sbb1 - 1;
			}
			else ge = 1;
			Conut = 0;
			for (i = n / 2 - 1; sb[i] == sbb1 && i >= 0; i --)
				Conut ++;
			for (i = n / 2; sb[i] == sbb2 && i < n; i ++)
				Conut ++;
			printf("%d %d %d\n", sbb1, Conut, ge);
		}
	}
	return 0;
}


你可能感兴趣的:(UVA - 10057 (中位数的运用))