雕刻时光

just do it……nothing impossible
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2011-04-20 21:57  huhuuu  阅读(313)  评论(0编辑  收藏  举报
列方程可知,最小点一定在给定的某点上
先排序
后枚举计算某点的权值
枚举可以由前面的推出后面的,不必重复计算
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);
}