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 }