UVA - 10057 - A mid-summer night's dream. (找中位数!)

UVA - 10057

Time Limit: 30000MSMemory Limit: Unknown64bit IO Format: %lld & %llu

Submit Status

Description

Download as PDF

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

______________________________________________________________________________________________ Shahriar Manzoor
16-12-2000

Source

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) ::  Volume 4. Algorithm Design
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Data Structures and Libraries :: Linear Data Structures with Built-in Libraries :: C++ STL algorithm (Java Collections)



做法:输入一组数,排序,找最中间那个(即中位数),然后分奇偶数讨论即可,注意最后一个数的意思是第一个中位数和第二个中位数之间数的个数


AC代码:


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int maxn = 1000005;
int num[maxn]; 

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 k = (n - 1) / 2;
		int n1 = 0, n2 = 1;
		if(n%2 == 1)
		{
			for(int i=k; i>=0 && num[k] == num[i]; i--) n1++;
			for(int i=k+1; i<n && num[k] == num[i]; i++) n1++;
		}
		else 
		{
			for(int i=k; i>=0 && num[k] == num[i]; i--) n1++;
			for(int i=k+1; i<n && num[k+1] == num[i]; i++) n1++;
			n2 = num[k+1] - num[k] +1;
		}
		printf("%d %d %d\n", num[k], n1, n2);
	}
	return 0;
} 




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值