uva 10057 - A mid-summer night's dream.
来源:互联网 发布:as3 js 交互 编辑:程序博客网 时间:2024/05/09 02:02
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:
210
10
4
1
2
2
4
Sample Output:
10 2 12 2 1
描述:
给定一组整数x1, x2,......,xn找到使S = |x1-A| + |x2-A| + ......+|xn-A|最小的A。
分析 :
可以先把整数排序,当A从负无穷大开始增大时,随着A左右整数的个数的变化,S也不断变化。当A左右整数相等时,S最小。
1.统计出每个整数出现的次数。
2.找到处于一半位置(设为h)的整数。
3.如果n是奇数或n是偶数但xh前面的数的个数加上xh的个数正好是一半时,则A只能是xh。
4.若n是偶数但xh前面的数加上xh的个数大于n的一半时,则有多个A满足条件。
代码:
- UVa 10057 - A mid-summer night's dream.
- uva 10057 - A mid-summer night's dream.
- uva 10057 - A mid-summer night's dream.
- UVa 10057 - A mid-summer night's dream.
- UVa 10057 - A mid-summer night's dream.
- UVA 10057 - A mid-summer night's dream.
- uva 10057 - A mid-summer night's dream.
- UVa:10057 A mid-summer night's dream
- UVA 10057 A mid-summer night's dream.
- uva 10057 - A mid-summer night's dream
- uva 10057 A mid-summer night's dream.(检索)
- uva 10057 A mid-summer night's dream.
- UVa 10057 - A mid-summer night's dream.
- uva 10057A mid-summer night's dream.
- uva 10057 - A mid-summer night's dream.
- UVA 10057 - A mid-summer night's dream.
- UVa 10057 - A mid-summer night's dream.
- UVA - 10057 A mid-summer night's dream.
- 九度OJ 1066: 字符串排序
- Oracle Exists 用法
- 弹球小游戏(JavaScript)
- uva 10057A mid-summer night's dream.
- 如何在exe文件中添加return语句
- uva 10057 - A mid-summer night's dream.
- ubuntu登录错误
- $(this)出了什么问题
- 有没有记录函数执行流程的OD插件?
- 计算机操作系统——进程管理(4)
- 1798.2683. Alice and Bob 看输入(棋盘边)是奇还是偶就可以了
- NorFlash和NandFlash区别
- vxWorks操作系统的映像及其装载过程
- 新手请教网页flash游戏的技术