HDU 2083 Simple version of the shortest distance

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=2083

Median, A,b,c is 3 points, if C is between a A and a, then C to a, B and for (B-A), if C<a or a>b, then C to a, b distance and certainly greater than (B-A).

So find the middle point on the line. If it is an odd number, the middle point is one, and if it is an even number, the two intermediate points will be selected.

The code is as follows:

#include <stdio.h>#include<math.h>#include<algorithm>using namespacestd;intMain () {intm;  while(SCANF ("%d", &m)! =EOF) {         while(m--)        {            intn,x[ -]; scanf ("%d",&N);  for(intI=0; i<n;i++) {scanf ("%d",&X[i]); } sort (X,x+N); intsum=0;  for(intI=0; i<n;i++) Sum+=abs (x[i]-x[n/2]); printf ("%d\n", sum); }    }    return 0;}

HDU 2083 Simple version of the shortest distance

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.