P1862输油管道问题
P1862输油管道问题
. . .
.
.
-----------------
.
. .
. . .
图像大体是这样,是带权平均数问题,设答案是k,sigama(abs(yi-k*wi))最小,这里的wi是1而已,问题就退化成了中位数。按纵坐标排序,求中位数即可。
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<cstring> 8 #define inf 2147483647 9 #define For(i,a,b) for(register long long i=a;i<=b;i++) 10 #define p(a) putchar(a) 11 #define g() getchar() 12 //by war 13 //2017.10.22 14 using namespace std; 15 struct node 16 { 17 long long x; 18 long long y; 19 bool operator<(const node &aa)const 20 { 21 return y<aa.y; 22 } 23 }a[10010]; 24 long long n; 25 long long s; 26 long long ans; 27 void in(long long &x) 28 { 29 long long y=1; 30 char c=g();x=0; 31 while(c<'0'||c>'9') 32 { 33 if(c=='-') 34 y=-1; 35 c=g(); 36 } 37 while(c<='9'&&c>='0')x=x*10+c-'0',c=g(); 38 x*=y; 39 } 40 void o(long long x) 41 { 42 if(x<0) 43 { 44 p('-'); 45 x=-x; 46 } 47 if(x>9)o(x/10); 48 p(x%10+'0'); 49 } 50 int main() 51 { 52 in(n); 53 For(i,1,n) 54 in(a[i].x),in(a[i].y); 55 sort(a+1,a+n+1); 56 s=a[(n+1)/2].y; 57 For(i,1,n) 58 ans+=abs(s-a[i].y); 59 o(ans); 60 return 0; 61 }