51NOD 1110 Distance and minimum V3 (median + tip)

Source: Internet
Author: User

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", &AMP;A[I].X,&AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.