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\n",m2);
49         else printf("%.5f\n",(m1 + m2) / 2);
50 
51         return 0;
52 
53 
54 
55 
56 }
View Code

 

posted @ 2014-03-08 21:29  hyx1  阅读(213)  评论(0编辑  收藏  举报