码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10057 A mid-summer night’s dream(中位数)

时间:2015-02-12 09:22:42      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:c语言   uva   acm   

                             uva 10057 A mid-summer night’s dream


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,和n个数中可以满足A的个数,以及可以满足A的整数个数。

解题思路:如果数字个数为奇数,那么中间那个就是唯一的中位数,如果是偶数,则会有中间的两位数,而且在这两位数之间的数也是。




#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int num[10000005];
int main() {
	int n;
	while (scanf("%d", &n) == 1) {
		for (int i = 0; i < n; i++) {
			scanf("%d", &num[i]);
		}
		sort(num, num + n);
		int cnt = 0;
		if (n % 2) {
			for (int i = 0; i < n; i++) {
				if (num[i] == num[n / 2]) cnt++;
			}
			printf("%d %d 1\n", num[n / 2], cnt);
		} else {
			for (int i = 0; i < n; i++) {
				if (num[i] == num[n / 2] || num[i] == num[(n / 2) - 1]) cnt++;
			}
			printf("%d %d %d\n", num[(n / 2) - 1], cnt, num[n / 2] - num[(n / 2) - 1] + 1);
		}
	}
	return 0;
}


uva 10057 A mid-summer night’s dream(中位数)

标签:c语言   uva   acm   

原文地址:http://blog.csdn.net/llx523113241/article/details/43741627

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!