前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 114 - Telecasting station

ACMSGURU 114 - Telecasting station

作者头像
Reck Zhang
发布2021-09-01 11:41:53
5140
发布2021-09-01 11:41:53
举报
文章被收录于专栏:Reck ZhangReck Zhang

Telecasting station

Problem Description

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

代码语言:javascript
复制
4
1 3
2 1
5 2
6 2

Sample Output

代码语言:javascript
复制
3.00000

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>
 
int main () {
    std::ios::sync_with_stdio(false);
 
    int n;
    std::cin >> n;
 
    double p_left = 0;
    double p_right = 0;
    double s_left = 0;
    double s_right = 0;
 
    std::vector<std::pair<double, double>> cities(n, std::pair<double, double>());
    for(auto& city : cities) {
        std::cin >> city.first >> city.second;
    }
 
    std::sort(cities.begin(), cities.end(), [](std::pair<double, double> a, std::pair<double, double> b) {
        return a.first < b.first;
    });
 
    for(int i = 0; i < n; i++) {
        p_right += cities[i].second;
        s_right += cities[i].first * cities[i].second;
    }
 
    double sum = LONG_LONG_MAX;
    double index = cities[0].first;
 
    for(int i = 0; i < n - 1; i++) {
        p_right -= cities[i].second;
        p_left += cities[i].second;
        s_right -= cities[i].first * cities[i].second;
        s_left += cities[i].first * cities[i].second;
        double a = cities[i].first * (p_left - p_right) + (s_right - s_left);
        double b = cities[i + 1].first * (p_left - p_right) + (s_right - s_left);
        if(sum - a > 1e-8) {
            sum = a;
            index = cities[i].first;
        }
 
        if(sum - b > 1e-8) {
            sum = b;
            index = cities[i + 1].first;
        }
    }
 
    std::cout<<std::setiosflags(std::ios::fixed);
    std::cout<<std::setprecision(5) << index << std::endl;
 
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Telecasting station
    • Problem Description
      • Input
        • Output
          • Sample Input
            • Sample Output
              • Solution
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档