1110 Distance and minimum V3
Base time limit: 1 seconds Space limit: 131072 the KBX axis has n points, each of which includes a positional data x[i] and a weight 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 10 17 19 1
Output example
20
Idea: Median.
The weight of a point is counted as multiple points on the line. Then the median is calculated.
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <math.h>5 using namespacestd;6typedefstructnode7 {8 intx;9 intCost ;Ten } SS; OneSS id[20000]; A BOOLCMP (node P,node q) - { - returnp.x<q.x; the } -typedefLong LongLL; - intMainvoid) - { + intn,i,j; -scanf"%d",&n); +LL sum =0; A for(i =0; I < n; i++) at { -scanf"%d%d",&id[i].x,&id[i].cost); -Sum + =Id[i].cost; - } -LL mid = (sum+1)/2; -Sort (id,id+n,cmp); inLL AK =0; - for(i =0; I < n; i++) to { +ak+=Id[i].cost; - if(AK >=mid) the { * Break; $ }Panax Notoginseng } - intX =id[i].x;sum =0; the for(i =0; i < N;i + +) + { ASum + = (LL) ABS (id[i].x-x) *Id[i].cost; the } +printf"%lld\n", sum); - return 0; $}
1110 Distance and minimum V3