51Nod 1110 距离之和最小 V3

                           1110 距离之和最小 V3
X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i]。点P到点P[i]的带权距离 = 实际距离 * P[i]的权值。求X轴上一点使它到这N个点的带权距离之和最小,输出这个最小的带权距离之和。
 
Input
第1行:点的数量N。(2 <= N <= 10000)
第2 - N + 1行:每行2个数,中间用空格分隔,分别是点的位置及权值。(-10^5 <= X[i] <= 10^5,1 <= W[i] <= 10^5)
Output
输出最小的带权距离之和。
Input示例
5
-1 1
-3 1
0 1
7 1
9 1
Output示例
20

 思路:网上这题有人用三分! 蒟蒻不会三分 

   把点权看成这个点的数量 表示有多少个位置为x的点 

   排序 找中位数

 1 #include <cstdio>
 2 #include <cctype>
 3 #include <algorithm>
 4 
 5 typedef long long LL;
 6 
 7 const int MAXN=10010;
 8 
 9 int n,pos;
10 
11 struct node {
12     int x,y;
13     friend inline bool  operator < (node x,node y) {
14         return x.x<y.x;
15     }
16 };
17 node e[MAXN];
18 
19 inline void read(int&x) {
20     int f=1;register char c=getchar();
21     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
22     for(;isdigit(c);x=x*10+c-48,c=getchar());
23     x=x*f;
24 }
25 
26 int hh() {
27     read(n);
28     
29     LL sum=0;
30     for(register int i=1; i<=n; ++i) read(e[i].x),read(e[i].y),sum+=(LL)e[i].y;
31         
32     std::sort(e+1,e+1+n);
33     LL t=(sum+1)/2;
34     sum=0;
35     for(int i=1; i<=n; ++i) {
36         sum+=(LL)e[i].y;
37         if(sum>=t) {
38             pos=e[i].x;
39             break;
40         }
41     }
42     LL ans=0;
43     for(int i=1; i<=n; ++i) ans+=(LL)abs(e[i].x-pos)*e[i].y;
44     printf("%lld
",ans);
45     
46     return 0;
47 }
48 
49 int sb=hh();
50 int main(int argc,char**argv) {;}
代码
原文地址:https://www.cnblogs.com/whistle13326/p/7709828.html