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

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;
}
时间: 2024-10-29 19:10:31

uva 10057 A mid-summer night’s dream(中位数)的相关文章

UVA - 10057 A mid-summer night&#39;s dream.

偶数时,中位数之间的数都是可以的(包括中位数) 奇数时,一定是中位数 推导请找初中老师 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; int box[1000000]; int main() { //freopen("in","r"

UVA 11300 Spreading the Wealth(技巧:中位数 推公式)

题目链接Spreading the Wealth A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the to

BestCoder#3

1001:Task schedule 思路:二分空余时间,注意二分的边界. #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string> #include <algorithm> #include <queue> using namespace std; const int maxn =200000+

[TJOI2010]中位数

题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从小到大排序后处在中间位置的数.(若序列长度为偶数,则指处在中间位置的两个数中较小的那个) 例1:1 2 13 14 15 16 中位数为13 例2:1 3 5 7 10 11 17 中位数为7 例3:1 1 1 2 3 中位数为1 输入输出格式 输入格式: 第一行为初始序列长度N.第二行为N个整数,

双堆求中位数

堆 堆的动态创建与删除可参考 http://www.java3z.com/cwbwebhome/article/article1/1362.html?id=4745,此处不再赘述. 双堆求中位数 算法描述: 1.创建两个堆(一个小根堆.一个大根堆),堆大小至少为给定数据个数的一半,向上取整: 2.假定变量mid用来保存中位数,取定第一个元素,赋值给mid,即作为初始的中位数: 3.依次遍历后面的每一个数据,如果比mid小,则插入大根堆:否则插入小根堆: 4.如果大根堆和小根堆上的数据个数相差为2

python使用二分法实现在一个有序列表中查找指定的元素

二分法是一种快速查找的方法,时间复杂度低,逻辑简单易懂,总的来说就是不断的除以2除以2... 例如需要查找有序list里面的某个关键字key的位置,那么首先确认list的中位数mid,下面分为三种情况: 如果 list[mid] < key,说明key 在中位数的 右边: 如果 list[mid] > key,说明key 在中位数的 左边: 如果 list[mid] = key,说明key 在中位数的中间: 范围每次缩小一半,写个while的死循环知道找到为止. 二分法查找非常快且非常常用,但

P3871 [TJOI2010]中位数

题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从小到大排序后处在中间位置的数.(若序列长度为偶数,则指处在中间位置的两个数中较小的那个) 例1:1 2 13 14 15 16 中位数为13 例2:1 3 5 7 10 11 17 中位数为7 例3:1 1 1 2 3 中位数为1 输入输出格式 输入格式: 第一行为初始序列长度N.第二行为N个整数,

堆--P1168 中位数

题目描述 给出一个长度为N的非负整数序列Ai?,对于所有1≤k≤(N+1)/2,输出A1,A3,…,A2k−1的中位数.即前1,3,5,…个数的中位数. 输入格式 第1行为一个正整数N,表示了序列长度. 第2行包含N个非负整数Ai(Ai≤109). 输出格式 共(N+1)/2行,第i行为A1,A3,…,A2k−1?的中位数. 首先记录一个变量mid,记录答案(中位数).建立两个堆,一个大根堆一个小根堆,大根堆存≤mid的数,小根堆存 >mid的的数. 所以我们向堆中加入元素时,就通过与mid的比

Java小练习 数组的复制,反转及查找(线性,二分)

/* 算法: 数组的复制,反转,查找(线性查找,二分查找) */ import java.util.Scanner; public class arrayCopyReverseSearch { public static void main(String[] args) { String[] arr = new String[]{"AA", "BB", "CC", "DD", "EE", "FF&