UVa 10057:a mid-summer night ' s dream.

Source: Internet
Author: User
Tags integer integer numbers sort


Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=998

Original title:

This is year 2200AD. Has progressed a lot in two hundred years. Two hundred years is mentioned here because this problem are being sent back to 2000AD with the help of time machine. Now it's possible to establish direct connection between Mans and computer CPU. People can watch other people ' s Dream on 3D Displayer (which is the monitor today) as if they were watching a movie. One problem in this century was that people have become then dependent on computers then their analytical ability is approach ing 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 are in great trouble as him has forgotten the number of his combination lock. For the 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. He records them and the help of his computer, Then hE has a clue that if the numbers are (X1, X2,   ...   Xn) He'll have to find a Combination lock code) such that

(| x1-a| + | x2-a| + ... + | xn-a|) is minimum.


Input would contain several blocks. Each block would start with a number n (0<n<=1000000) indicating how many numbers the him saw in the dream. Next there would be n numbers. All the numbers would be less than 65536. The input is terminated by end of.


For each set of input there is one line of output. That line would contain the minimum possible value for A. Next it would contain how many numbers are there in the "satisfy" the property of A (the summation of absolute dev Iation from A is minimum). And finally you have to print how many possible different the integer values are there for A (this values need not being present In the input). These numbers'll are separated by single.

Sample Input:

Sample output:10 2 1
2 2 1

Analysis and Summary:

Find A To make (| x1-a| + | x2-a| + ... + | xn-a|) The value is minimal.

So this a is actually the median number. So after the sort,

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

If the number of digits is odd, then the middle one is the only median, if it is even, there will be the middle of the two digits, and the number between the two digits is also.

In order to find out how many numbers in a series, it should be noted that the odd number is to find out the middle two, the next two numbers may have an equal number

* * uva:10057-a mid-summer night ' s dream. * time:0.236s * result:accept * * * * * * * * * * */#include <iostream> #include <cstdio> #inc  
Lude<algorithm> #define MAXN 1000010 using namespace std;  
int ARR[MAXN];  
    int main () {int n;  
        while (~SCANF ("%d", &n)) {for (int i=0; i<n; ++i) scanf ("%d", &arr[i]);  
        Sort (arr, arr+n);  
        int mid, left, right;  
        int ans_min, ans_num, Ans_val;  if (n%2==0) {//If there are even several digits left= (n-2)/2;  
            right= (n)/2;  
            for (int i=left; arr[i]==arr[left]&&i>=0;-I.) ++ans_num;  
            for (int i=right; arr[i]==arr[right]&&i<n; ++i) ++ans_num;  
        Ans_val = arr[right]-arr[left]+1;  
      } else{mid = (n-1)/2;
            Ans_min = Arr[mid];  
            for (int i=mid; arr[i]==arr[mid]&&i>=0;-I.) ++ans_num;  
            for (int i=mid+1; arr[i]==arr[mid]&&i<n; ++i) ++ans_num;  
        Ans_val = 1;  
    printf ("%d%d%d\n", Ans_min, Ans_num, Ans_val);  
return 0; }

Author: csdn Blog shuangde800

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.