当前位置:网站首页>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
边栏推荐
- R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、对匹配后的样本的不同分组对应的目标变量的均值进行Welch双样本t检验分析、双独立样本t检验
- Ganglia 的安装与部署
- Simpledateformat concrete classes for formatting and parsing dates
- Bubble sort
- 常用正则表达式
- 7-2 后序+中序序列构造二叉树
- RT-thread使用rt-kprintf
- Laravel add helper file
- Listen to the markdown file and hot update next JS page
- 【js】-【字符串-应用】- 学习笔记
猜你喜欢
![[JS] - [tree] - learning notes](/img/62/de4fa2a7c5e52c461b8be4a884a395.png)
[JS] - [tree] - learning notes
What you must know about time series database!

第六章 网络学习相关技巧5(超参数验证)

一文理解OpenStack网络

Yyds dry goods inventory tells us 16 common usage scenarios of redis at one go

MySQL 表的增删查改

Case analysis: using "measurement" to improve enterprise R & D efficiency | ones talk

国内有哪些好的智能家居品牌支持homekit?

Huawei machine learning service speech recognition function enables applications to paint "sound" and color

Latest development of jetpack compose
随机推荐
常用正则表达式
#22Map介绍与API
【js】-【字符串-应用】- 学习笔记
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用AIC函数比较两个模型的AIC值的差异(简单模型和复杂模型)
Continuous soul torture from two MySQL indexes of interviewers
Collation of Digital IC design experience (II)
2021-2022中国金融数字化“新”洞察行业研究报告
R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、对匹配后的样本的不同分组对应的目标变量的均值进行Welch双样本t检验分析、双独立样本t检验
【js】-【栈、队-应用】-学习笔记
[JS] - [string - application] - learning notes
Basic data type
冒泡排序
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用exp函数和coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比(odds ratio)
The dplyr package select function of R language moves the specified data column in the dataframe data to the first column (the first column) in the dataframe data column
379. 捉迷藏
【js】-【树】-学习笔记
Écoutez le fichier markdown et mettez à jour Hot next. Page JS
基于三维GIS开发的水电工程建设方案
Websocket learning
R language uses GLM function to build Poisson log linear regression model, processes three-dimensional contingency table data to build saturation model, uses summary function to obtain model summary s