uva 10057 - A mid-summer night's dream.

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

做这题想起了初中的数学老师,绝对值的知识都是她教的怀念ing

绝对值的几何意义代表两点间的距离;下面进行零点分类讨论
题目要求A到x1到xn的距离和的最小值
1.

                        |        |
数轴:1 2  .. X1 ....X2...    
显然:x1<=A<=X2;
2.
                       |         |         |       |
数轴:1 2  .. X1 ....X2...X3.....X4   
x1,x4一组,x2,x3一组
X2<=A<=x3,min=|x4-x1|+|x3-x2|;
假设A在x1,x2之间,A到x1,x4的距离为 |x4-x1|,到其X2,X3的距离大于|x3-x2|
A在其他位置同理;
所以数字个数为偶数个时,A在最中间的两个数字之间取值
3.
            |         |       |
1 2  .. X1 ....X2...X3....
x1,x3一组,x2单独一组;
A=x2;
A在x1,x3之间取值到端点距离不变,所以到x2的最近的点可取到最小值,A=x2

4.
           |          |       |          |         |
1 2  .. X1 ....X2...X3.....X4.....X5   
x1,x5一组,x2,x4一组,x3单独一组
A=x3;
同上A在x1,x5之间取值到分组后的端点距离不变,所以到x3的最近的点可取到最小值,A=x3
所以个数为奇数个时,A等于中位数;

#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,a[1000000];
int main()
{
 int i,min,max,sum,count;
 while (scanf("%d",&n)!=EOF)
 {
  for (i=0;i<n;i++)
  scanf("%d",&a[i]);
  sort(a,a+n);
  if (n%2) {sum=1;min=a[n/2]; max=min;}
      else {sum=a[n/2]-a[n/2-1]+1; min=a[n/2-1];max=a[n/2];}
  count=0;
  for (i=0;i<n;i++)
  {
   if ((a[i]>=min)&&(a[i]<=max)) ++count;
   if (a[i]>max) break;
  }
 printf("%d %d %d\n",min,count,sum);
 }
 return 0;
}

你可能感兴趣的:(Security,Integer,input,each,output,Numbers)