[中位数]A mid-summer night's dream.uva10057
来源:互联网 发布:企业查询软件下载 编辑:程序博客网 时间:2024/05/22 10:31
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
______________________________________________________________________________________________Shahriar Manzoor
16-12-2000
题意:我们的科学领导人遇到一个很大的问题,他忘掉了他的密码锁的号码,基于安全性考量,目前的电脑无法解决密码锁的问题。在一个仲夏夜里,科学家做了一个梦,梦里他看到许多数字飞来飞去。他把这些数字纪录在电脑上。接着他想到有一个线索,假如数字列为(X 1 , X 2 , … , X n)他必须找到一个整数A(A就是他密码锁的密码)使得( |X 1-A| + |X 2 -A| + … … + |X n -A|)为最小。
思路:很容易就能看出来这个数就是这个数组的中位数。只需区分偶数和奇数的情况就可以了。
数学都忘光了。这个都差点给忘了。。
#include<iostream>#include<algorithm>using namespace std;int arry[1000005];int n;int main(){ while(cin>>n) { int i,j,k; for(i=0;i<n;i++) cin>>arry[i]; sort(arry,arry+n); int minc,maxc,num=0,sum=0; if(n%2) { sum=1; minc=arry[n/2]; maxc=arry[n/2]; } else { sum=arry[n/2]-arry[n/2-1]+1; minc=arry[n/2-1]; maxc=arry[n/2]; } for(i=0;i<n;i++) { if(arry[i]>=minc&&arry[i]<=maxc) num++; if(arry[i]>maxc) break; } cout<<minc<<" "<<num<<" "<<sum<<endl; } return 0;}
- [中位数]A mid-summer night's dream.uva10057
- uva10057 - A mid-summer night's dream
- UVA10057-A mid-summer night's dream.
- uva10057 - A mid-summer night's dream
- A mid-summer night’s dream uva+二分/中位数
- UVA - 10057 A mid-summer night's dream. 求中位数
- UVA A mid-summer night’s dream(中位数求值)
- uva 10057 A mid-summer night’s dream(中位数)
- A mid-summer night’s dream - UVa 10057 中位数
- 10057 - A mid-summer night's dream.
- UVaOJ10057 - A mid-summer night's dream.
- 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.
- Tomcat启动web项目报Bad version number in .class file (unable to load class ...)错
- NDK与CDT编译
- WSO2-产品介绍
- Sizeof与Strlen的区别与联系
- 产生静态cubamap的脚本
- [中位数]A mid-summer night's dream.uva10057
- 一、总结几种关于ViewControllers之间传值的方式(从父视图向子视图传值)
- 守护进程的创建方法和步骤
- UML之行为图(活动图、状态图、序列图、协作图)
- 17章 树、堆和优先队列
- PHP版本的区别
- 张小龙:APP产品经理必懂的30条原则
- oracle 中trunc() 函数的学习
- vijos1010题解