Portal
There are n points on the x-axis, each of which includes a positional data x[i], and a weight value of w[i]. The weighted distance from the point to the other point = the actual distance * weight value. A point on the x-axis makes it to the minimum of the weighted distance of the N-point, and outputs the sum of the least-weighted distance.
Input
Line 1th: number of points N. (2 <= N <= 10000)
2-n + 1 lines: 2 numbers per line, separated by a space, respectively, the position and weight of the point. ( -10^5 <= x[i] <= 10^5,1 <= w[i] <= 10^5)
Output
Outputs the sum of the least weighted distances.
Input example
5
-1 1
-3 1
0 1
7 1
9 1
Output example
20
Problem Solving Ideas:
Because finally multiplied by the weights w[i], then we can imagine to have w[i] "x[i" coordinates, then we are converted to the median problem, first we will count the number of numbers (that is, all the weights added), and then divided by 2, and then according to small to large order, to find the most middle of the number, Find the coordinates to record that point X[i], and then the rest is simple, according to the meaning of the title to do it.
My Code:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstdlib>using namespace STD;typedef Long LongLL;Const intMAXN =1e4+5;structnode{LL x, W;} A[MAXN];BOOLCMP (Node A, Node B) {returna.x < b.x;}intMain () {intN while(~scanf("%d", &n)) {LL sum =0; for(intI=0; i<n; i++) {scanf("%i64d%i64d", &A[I].X,&A[I].W); sum + = A[I].W; } sum>>=1; Sort (A, a+n, CMP); LL ans =0, TMP = a[0].x; for(intI=0; i<n; i++) {ans + = A[I].W;if(Ans > sum) {tmp = a[i].x; Break; }} LL ret =0; for(intI=0; i<n; i++) ret + =ABS(a[i].x-tmp) *A[I].W;printf("%i64d\n", ret); }return 0;}
51NOD 1110 Distance and minimum V3 (median + tip)