Vijos/Question Bank/oil pipeline problem, vijos Oil Pipeline

Source: Internet
Author: User

Vijos/Question Bank/oil pipeline problem, vijos Oil Pipeline
Background

Miss the car's GF, and the car has a problem!

Description

An oil company plans to build a main oil pipeline from east to west. The pipeline needs to pass through an oil field with n wells. Each well must have an oil pipeline along the shortest path (or south or north) connected to the main pipeline. How to determine the optimal position of the main pipeline if n wells are given, that is, their x coordinates (East-West) and y coordinates (north-south direction, even if the total length of the oil pipeline from each well to the main pipeline is the smallest position?

Programming task:
Given the location of n wells, the minimum length of the oil pipeline between each well and the main pipeline is calculated by programming.

Format input format

The number of wells in the input row is n, 1 ≤ n ≤ 1st.

The next n rows are the locations of the wells. Each row has two integers x and y,-10000 ≤ x, and y ≤ 10000.

Output Format

The number in the output 1st rows is the sum of the minimum length of the oil pipeline between the oil well and the main pipeline.

Example 1 input 1
51 22 21 3 3 -23 3
Sample output 1
6
Restrictions

1 s for each test point

Prompt

1 s for each test point

Source

Classic Algorithms

 

 1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 using namespace std; 5 int a[10001]; 6 int b[10001]; 7 int tot; 8 int main() 9 {10     int n;11     cin>>n;12     for(int i=1;i<=n;i++)13     {14         cin>>a[i]>>b[i];15     }16     sort(b+1,b+n+1);17     int mid=b[n/2+1];18     for(int i=1;i<=n;i++)19     {20         tot=tot+abs(b[i]-mid);21     }22     cout<<tot;23     return 0;24 }

 

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.