zoukankan      html  css  js  c++  java
  • ACDream 1735 输油管道

    输油管道

    Time Limit: 2000/1000MS (Java/Others) Memory Limit: 262144/131072KB (Java/Others)

    Problem Description

    平面上有n个油井,现在要建立一条主干线,用来把所有的油井产出的原油都输送出去,主干线是平行于x轴的一条直线,每个油井通过一条支线把原油输送到主干线上,现在给定n个油井在平面上的坐标,那么应该把主干线建在什么地方才能让所有的支干线的总长度最小呢?

    Input

    首先一个正整数n,接下来n行每行两个整数,代表n个油井在平面上的位置。n和坐标都是小于等于1000000的正整数。

    Output

    输出总的支干线长度的最小值,每个结果占一行。

    Sample Input

    2
    0 0
    10 10

    Sample Output

    10

    Manager

     
    题意:给出点的坐标,求最小的输油管的长度。
    分析:可以转化成x坐标上的一些固定的点,求一个点到这些点的距离最小。可以证明这个点的坐标的是这些点的中位数。
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stdlib.h>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    #define LL long long
    using namespace std;
    const int MAXN=1000000+5;
    int a[MAXN];
    int main()
    {
        int n,temp,x;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d %d",&temp,&a[i]);
        sort(a,a+n);
        if(n%2) x=a[n/2];
        else x=(a[n/2]+a[n/2-1])/2;
        LL ans=0;
        for(int i=0;i<n;i++)
            ans+=(LL)abs(x-a[i]);
        printf("%lld
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    依赖单元测试开发
    今天晚上的遭遇
    设计,UML,测试驱动开发
    我是LIGHT的LP,今天由我代笔
    转贴一篇关于BitVector32的Blog
    看牙记
    调整过的书籍目录
    Queue和Stack的学习代码
    BitVector32结构学习
    Visual Studio 2008 在64位操作系统上调试代码的解决方式
  • 原文地址:https://www.cnblogs.com/clliff/p/4490604.html
Copyright © 2011-2022 走看看