Sgu 114
|
114. Telecasting station time limit per test: 0.25 sec. memory limit per test: 4096 KB 开始还自己一个一个的枚举想3分每个区间段,找最优,后来发现是线性的,看是大于还是小于0去两边,结果发现没加上取左边的,每次都是按取右边的在算,所以还是有问题,不过记得之前看过白书说中位点最优!- - 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 |
||||||
|
1 #pragma comment(linker,"/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <vector> 4 #include <cmath> 5 #include <queue> 6 #include <set> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 #define INF 0x7fffffff 12 #define mod 1000000007 13 #define ll long long 14 #define maxn 100025 15 #define pi acos(-1.0) 16 int n, m; 17 struct node{ 18 ll x, c; 19 }a[maxn]; 20 bool cmp(node a, node b){ 21 return a.x < b.x; 22 } 23 int main(){ 24 scanf("%d", &n); 25 ll s = 0,cnt=0; 26 for (int i = 0; i < n;i++){ 27 scanf("%lld%lld", &a[i].x, &a[i].c); 28 cnt += a[i].c; 29 } 30 sort(a, a + n, cmp); 31 int p = 0; 32 double ans = 0; 33 for (int i = 0; i < n; i++){ 34 s += a[i].c; 35 if (s >= cnt / 2){p = i; break;} 36 } 37 ans = a[p].x; 38 if (cnt % 2 == 2&&s==cnt/2)ans = ((double)(a[p].x + a[p + 1].x))/2; 39 printf("%.5lf\n", ans); 40 return 0; 41 }
浙公网安备 33010602011771号