当前位置:网站首页>7-6 laying oil well pipeline
7-6 laying oil well pipeline
2022-06-24 23:31:00 【White -】
7-6 Laying oil well pipelines
An oil company has n Well , To facilitate the transportation of oil , Plan to build an oil pipeline . According to the design requirements , There is a main pipe in the horizontal direction , A vertical branch pipeline is built for each oil well to lead to the main pipeline . Please design an algorithm to determine the location of the main pipeline , Minimize the total length of branch pipeline from all oil wells to the main pipeline . Tips : The complexity is O(n) To pass all test cases .
Input format :
Each input file is a test case , The first line of each file gives a positive integer n(1≤n≤1000000), Indicates the number of wells , From the second line n Row data , Indicates the location of each well , Each line contains two integers separated by spaces , Represent the abscissa of each oil well respectively x(−10000≤x≤10000) And the vertical y(−10000≤y≤10000).
Output format :
Output the sum of the minimum length of the branch pipeline from each oil well to the main pipeline .
sample input :
Here's a set of inputs . for example :
4
-1 1
2 2
5 -1
-3 7
sample output :
9
Code :
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int n;
int a[1001010];
int k;
int mid;
void fid(int left,int right)
{
int temp=a[left];
int i=left;
int j=right;
if(i>j)
return;
while(i<j)
{
while(i<j&&a[j]>temp)
j--;
a[i]=a[j];
while(i<j&&a[i]<=temp)
i++;
a[j]=a[i];
}
a[i]=temp;
if(i==k)
mid=a[i];
else if(i>k)
fid(left,i-1);
else
fid(i+1,right);
return ;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d%d",&x,&a[i]);
}
if(n%2==0)
k=n/2;
else
k=n/2+1;
fid(1,n);
long long sum=0;
for(int i=1;i<=n;i++)
sum+=abs(a[i]-mid);
printf("%lld",sum);
return 0;
}
202206222114 3、 ... and
边栏推荐
猜你喜欢

Hydropower project construction scheme based on 3D GIS Development

当初吃土建起来的“中台”,现在为啥不香了?

2021-2022中国金融数字化“新”洞察行业研究报告

7-7 数字三角形

【js】-【树】-学习笔记

Design and practice of vivo server monitoring architecture
Unveiling the secrets of the Winter Olympics | smartbi's partners supported the "front and back" of the Beijing Winter Olympics
![[introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy](/img/d0/7d78b00e4f6ad1e8efb73a5d472b09.png)
[introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy

Harmonyos accessing database instances (3) -- use ORM bee to test how good harmonyos is
What you must know about time series database!
随机推荐
7-3 最大子段和
376. Tâches mécaniques
Common regular expressions
2021-2022中国金融数字化“新”洞察行业研究报告
Go language pointer, value reference and pointer reference
websocket长链接压测
Use of laravel verifier
idea创建模块提示已存在
[JS] - [string - application] - learning notes
[JS] - [array application] - learning notes
Detailed explanation of online group chat and dating platform project (servlet implementation)
Laravel authentication module auth
What good smart home brands in China support homekit?
RT-thread使用rt-kprintf
Collation of Digital IC design experience (II)
376. 機器任務
文件包含漏洞问题
Continuous soul torture from two MySQL indexes of interviewers
InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog
golang map clear