1110 Distance and minimum V3

Source: Internet
Author: User

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

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.