ACdream 1735 输油管道 (排序)

http://acdream.info/problem?pid=1735

官方题解:http://acdream.info/topic?tid=4246

因为主干线是平行于x轴的直线,那么跟x坐标其实没关系,考虑两个点时直线是位于两点的中部,三个点时有一个点会位于直线上,那么,奇数个点是一定有一个点位于直线上的,偶数个点的话是位于两个点的中心。

输入之后对y排序输出即可。

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 const int N = 1000010;
 5 int b[N];
 6 int main()
 7 {
 8     int n,a;
 9     scanf("%d",&n);
10     for(int i=0;i<n;i++)
11         scanf("%d%d",&a,&b[i]);
12     sort(b,b+n);
13     long long ans=0;
14     for(int i=0;i<n/2;i++)
15     {
16         ans+=b[n-i-1]-b[i];
17     }
18     printf("%lld
",ans);
19     return 0;
20 }
原文地址:https://www.cnblogs.com/nowandforever/p/4491712.html