sgu 114 Telecasting station

39 篇文章 0 订阅
18 篇文章 0 订阅

题目描述:

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[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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值