zoukankan      html  css  js  c++  java
  • SGU[114] Telecasting station

    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.

    Berland的每个城市都在Ox轴上。这个国家的政府决定建立一个新的广播电视站。经过很多次的实验以后,Berland的科学家得出了一个结论:所有城市的不满意度等于城市数乘以它们到广播电视站的距离。在Ox轴上找到这样的一个广播电视站使得所有城市不满意度最低。

     

    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).

    输入第一行包含一个正数N (0 < N < 15000) ——表示Berland的城市数。接下来N对 (X, P) 描述城市 (0 < X, P < 50000),其中X是城市的坐标,P表示城市数。所有的数字以空格分隔。


    Output

    输出

    Write the best position for TV-station with accuracy 10-5.

    输出广播电视站的最佳位置,精确到10-5


    Sample Input

    样例输入

    4

    1 3

    2 1

    5 2

    6 2


    Sample Output

    样例输出

    3.00000

     

    Analysis

    分析

    这道题目有几个地方需要注意:

    1. 最后精确到10-5的要求基本是多余的,只要在结果后面再输出“.00000”;
    2. 本题使用了Special Judge,因此答案可能不唯一。

    有了上面两点的认识,我们来考虑一般的解决方案。我们不妨考虑电视台建在城市中,很容易证明,这也是这个问题的一个解。

    我们不妨把所有的城市列出来,比如样例输入中,一共有8个城市,我们把它们认为是不同的,并且列成一排:

    (1, 1)、(1, 2)、(1, 3)、(2, 4)、(5, 5)、(5, 6)、(6, 7)、(6, 8)

    其中第一个数字表示城市的位置,第二个数字表示第几个城市。很容易发现,我们需要求的是最中间的那个城市。

    如果城市数目为奇数,那么我们所需要求的就是第S / 2 + 1个城市,其中S表示城市总数。

    如果城市数目为偶数,那么我们可以输出第S / 2个城市或第S / 2 + 1个城市,为了方便起见,我们都取第S / 2 + 1个城市,这并不会对答案的这正确性产生影响。

     

    Solution

    解决方案

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    struct City
    {
    	City(int _P = 0, int _M = 0)
    	{ P = _P; M = _M; }
    
    	int P, M;
    };
    
    int cmp(City x, City y)
    { return x.P < y.P; }
    
    vector<City> pCity;
    
    int main()
    {
    	int N, P, M;
    	while(cin >> N)
    	{
    		int nCnt = 0;
    		pCity.clear();
    		for(int i = 1; i <= N; i++)
    		{
    			cin >> P >> M;
    			pCity.push_back(City(P, M));
    			nCnt += M;
    		}
    		sort(pCity.begin(), pCity.end(), cmp);
    		int nTmp = nCnt / 2 + 1;
    		int nPos = 0;
    		for(; nTmp > 0; nPos++)
    		{
    			if(nTmp - pCity[nPos].M > 0) { nTmp -= pCity[nPos].M; }
    			else { break; }	
    		}
    		cout << pCity[nPos].P << ".00000" << endl;
    	}
    	return 0;
    }
    

      

    这道题目主要是一开始说明的两点要注意一下,否则会WA很多次。

  • 相关阅读:
    python学习-10 运算符1
    python学习-9 pycharm的安装
    python学习-8 用户有三次机会登陆
    python学习-7 条件语句 while循环 + 练习题
    python学习-6 猜拳小游戏
    python学习-5 python基础-2 条件语句(if的简单用法2---elif)
    python学习-4 python基础-2 条件语句(if的简单用法1)
    python学习-3 python基础-1基础知识和解释器
    RaspBerry--解决无法用 ssh 直接以 root 用户登录
    NetWork--HTTPS 原理解析<转>
  • 原文地址:https://www.cnblogs.com/Ivy-End/p/4335835.html
Copyright © 2011-2022 走看看