zoukankan      html  css  js  c++  java
  • SGU 114

    114. Telecasting station

    time limit per test: 0.25 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 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 (XP) 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

    把权值看成有多少个人,求中位数。
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 #define maxn 15005
     9 
    10 struct node  {
    11 
    12         int x,p;
    13 };
    14 
    15 int n;
    16 node s[maxn];
    17 
    18 bool cmp(node a,node b) {
    19         return a.x < b.x;
    20 }
    21 
    22 int main() {
    23         //freopen("sw.in","r",stdin);
    24 
    25         scanf("%d",&n);
    26 
    27         int sum = 0;
    28         for(int i = 1; i <= n; ++i) {
    29                 scanf("%d%d",&s[i].x,&s[i].p);
    30                 sum += s[i].p;
    31         }
    32 
    33         sort(s + 1,s + n + 1,cmp);
    34 
    35         int now = 0;
    36         double m1,m2;
    37         for(int i = 1; i <= n; ++i) {
    38                 now += s[i].p;
    39                 if(now >= sum / 2) {
    40                         m1 = s[i].x;
    41                         if(now >= sum / 2 + 1) m2 = s[i].x;
    42                         else m2 = s[i + 1].x;
    43                         break;
    44                 }
    45 
    46         }
    47 
    48         if(sum % 2) printf("%.5f
    ",m2);
    49         else printf("%.5f
    ",(m1 + m2) / 2);
    50 
    51         return 0;
    52 
    53 
    54 
    55 
    56 }
    View Code
  • 相关阅读:
    代码—五天前是星期几
    随手笔记-日期
    建造曲线记忆表格
    2017-5-10随手记
    java-web——第一课 tomcat
    复习
    Leetcode 24. Swap Nodes in Pairs(详细图解一看就会)
    这可能是最详细的解析HTTP走私攻击的文章
    使用grub2引导进入Linux或Window系统
    ACM集训第一次积分赛赛前复习+day4
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3588647.html
Copyright © 2011-2022 走看看