带权中位数 + 二分 对那样例特无语!!!!!! (本题是Special Judge 跟样例不一样也能过啦!!!!!) 114. 电视广播站 时限: 0.50 sec 空间: 4096 KB Berland国家的城市分布在数轴上。国家政府想要建一个新的电视广播站。在进行了很多次调研以后Berland得出了一些结论:每一个城市的不愉快值等于城市人口乘上城市与广播站的距离。在数轴上找到这一点使得所有城市的不愉快值总和最小。 输入 首先是一个正整数 N(0<N<15000) 代表Berland的城市总数。接下来N个数对(X, P)分别描述N个城市 (0<X, P<50000) ,X是城市位置,P是城市人口。数据被空格隔开。 输出 广播站最佳的位置,精确到10-5。 样例输入 4 1 3 2 1 5 2 6 2 样例输出 3.00000 #include <iostream> #include <algorithm> using namespace std; struct node { int x; int y; }; node a[15000]; bool cmp(node a, node b) { return a.x < b.x; } int f(int from, int to) { if(from > to) return (a[from].x+a[to].x)/2; if(from == to) return a[from].x; if(a[from].y == a[to].y) { a[from].y = a[to].y = 0; from++; to--; return f(from, to); } if(a[from].y > a[to].y) { a[from].y -= a[to].y; to--; return f(from, to); } a[to].y -= a[from].y; from++; return f(from, to); } int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d %d", &a[i].x, &a[i].y); } sort(a+1, a+1+n, cmp); cout << f(1, n) << ".00000" << endl; return 0; }