sgu 114 Telecasting station

来源:互联网 发布:甄欣的淘宝店叫什么? 编辑:程序博客网 时间:2024/05/01 13:00

题目描述:

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 citizensdispleasure 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 ofdispleasures of all cities is minimal.

Input

Input begins from line with integer positive numberN (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 andP is an amount of citizens. All numbers separated by whitespace(s).

Output

Write the best position for TV-station with accuracy10-5.

Sample Input

41 32 15 26 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[n]-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[n]-sum[i]){                break;    }    printf("%.5lf\n",a[i].first);    return 0;}


原创粉丝点击