您的位置:首页 > 其它

sgu 114 Telecasting station

2012-10-26 10:31 288 查看
题目描述:


114. Telecasting station

time limit per test: 0.5 sec.

memory limit per test: 4096 KB
Every city in Berland is situated on Ox axis. The government of the country decided to build new telecasting station. After many experiments Berland scientists came to a conclusion that in any city citizens
displeasure is equal to product of citizens amount in it by distance between city and TV-station. Find such point on Ox axis for station so that sum of
displeasures of all cities is minimal.
Input
Input begins from line with integer positive number
N (0<N<15000) – amount of cities in Berland. Following N pairs
(X, P) describes cities (0<X, P<50000), where X is a coordinate of city and
P is an amount of citizens. All numbers separated by whitespace(s).
Output
Write the best position for TV-station with accuracy
10-5.
Sample Input
4
1 3
2 1
5 2
6 2

Sample Output
3.00000


原来又傻B地想去三分。。。不知道为什么会有这么奇怪的想法。

后来小推下最优的公式,发现一个奇妙的现象。

我们先退一步思考,假设只能区给定的点。

而且目前最优解在val = a的地方取到。 且它之前点的val和为sum1 ,后面的和为sum2 。

---------------.---------------

<--sum1-->a<--sum2-->

假设点a后某段的值更优

那么值为 f(a) + sum1*deltx+a*deltx-sum2*deltx;

就是需要 a+sum1-sum2<0;

所以就是找到某个点 sum[i]>=sum
-sum[i]

答案呼之欲出!

代码来了:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))

using namespace std;
int const nMax = 40000;
typedef int LL;
typedef pair<LL,LL> pij;
double const eps=1e-5;

int n;
typedef pair<double ,double> Int;
Int a[nMax];
double sum[nMax];

int main()
{
scanf("%d",&n);
a[0].first=0;
a[0].second=0;
for(int i=1;i<=n;i++){
scanf("%lf%lf",&a[i].first,&a[i].second);
}
sort(a+1,a+n+1);
sum[0]=0;
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i].second;
int i;
for(i=1;i<=n;i++)if(sum[i]>=sum
-sum[i]){
break;
}
printf("%.5lf\n",a[i].first);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: