哪一点的位置权值最小——sgu114. Telecasting station

列方程可知,最小点一定在给定的某点上
先排序
后枚举计算某点的权值
枚举可以由前面的推出后面的,不必重复计算
哪一点的位置权值最小——sgu114. Telecasting station View Code
   
     
#include < stdio.h >
#include
< iostream >
#include
< algorithm >
using namespace std;

struct data
{
double x;
int p;
}map[
15099 ];

int cmp(data a,data b)
{
return a.x < b.x;
}

int main()
{
int n;
scanf(
" %d " , & n);

int i;
for (i = 0 ;i < n;i ++ )
{
scanf(
" %lf %d " , & map[i].x, & map[i].p);
}

if (n == 1 )
{
printf(
" %.5lf\n " ,map[ 0 ].x);
return 0 ;
}
sort(
& map[ 0 ], & map[n],cmp);

double min = 0 ;
double rx = map[ 0 ].x,lp = 0 ,rp = map[ 0 ].p;
for (i = 1 ;i < n;i ++ )
{
min
+= (map[i].x - rx) * map[i].p;
rp
+= map[i].p;
}

double add = min,cha;
for (i = 1 ;i < n;i ++ )
{
lp
+= map[i - 1 ].p;
rp
-= map[i - 1 ].p;

cha
= map[i].x - map[i - 1 ].x;
add
= add - cha * rp + cha * lp;
if (add < min)
{
rx
= map[i].x;
min
= add;
}
}

printf(
" %.5lf\n " ,rx);
}

你可能感兴趣的:(cast)