Ultraviolet A 10057-a mid-summer night's dream.

Source: Internet
Author: User
Tags integer numbers

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
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
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(ThisAIs 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. next it will contain How many numbers are there in the input that satisfy the property of a (
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

This reminds me of the mathematics teacher in junior high school. The absolute value of knowledge is what she taught.

The geometric meaning of the absolute value represents the distance between two points. The following is a discussion of the null classification.
The minimum value of the distance from A to X1 to XN is required.
1.

|
Number axis: 1 2... X1... x2...
Apparently: X1 <= A <= x2;
2.
|
Number axis: 1 2... X1... x2.... x3.... X4
X1, X4, X2, X3
X2 <= A <= X3, min = | x4-x1 | + | x3-x2 |;
Suppose a is in x1, x2, A to X1, X4 distance is | x4-x1 |, to its X2, X3 distance greater than | x3-x2 |
A is the same in other locations;
Therefore, when the number of digits is an even number, A is taken between the two numbers at the center.
3.
|
1 2... X1... x2.... x3 ....
X1 and x3. x2. x2;
A = x2;
The distance between A and X3 is the same as the distance from the endpoint. Therefore, the minimum value can be obtained from the nearest vertex of X2, A = x2.

4.
|
1 2. X1... x2.... x3.... x4..x5
X1, X5, X2, X4, X3, and x3.
A = X3;
The distance between A and X5 is the same as that between A and X1. Therefore, the minimum value can be obtained for the closest vertex to X3. A = X3
So when the number is an odd number, A is equal to the median;

#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.