UVA - 10057 A mid-summer night's dream.

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


题目大意:输入n个数,现在要你求出
最小的(|X1-A| + |X2-A| + ... + |Xn-A|)
A是X1,X2...Xn中的某个数,即中位数
现在要你求出最小的A,A的个数,A的种类数(可以和n个数不同)

解析:

当n为奇数时,A就是中间那个数,for一遍查找一下A,就是A的个数,A的种类数为1。
当n为偶数时,A就是中间的两个数,for一遍查找中间两个数有多少个,就是A的个数,A的种类数为两个数中间个数+1。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1000005;
int num[N];
int main() {
	int n;
	while(scanf("%d",&n) != EOF) {
		for(int i = 0; i < n; i++) {
			scanf("%d",&num[i]);
		}
		sort(num,num+n);
		int ans[2],cnt;
		cnt = 0;
		if(n % 2) {
			ans[0] = num[n/2];
			printf("%d ",ans[0]);
			for(int i = 0; i < n; i++) {
				if(num[i] == ans[0]) {
					cnt++;
				}
			}
			printf("%d 1\n",cnt);
		}else {
			ans[0] = num[n/2-1];
			ans[1] = num[n/2];
			printf("%d ",ans[0]);
			for(int i = 0; i < n; i++) {
				if(num[i] == ans[0] || num[i] == ans[1]) {
					cnt++;
				}
			}
			printf("%d %d\n",cnt,ans[1]-ans[0]+1);
		}
	}
	return 0;
}


你可能感兴趣的:(uva,a,10057,nights,mid-summer)