您的位置:首页 > 其它

SGU 114 Telecasting station

2016-08-06 16:57 423 查看
题目:点击查看相似题目(我的博客)

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.

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


用三分来做是可以的。

因为函数f一定是凹函数。

代码:

#include<iostream>
#include<string.h>
using namespace std;

int list1[15001];
int list2[15001];
int n;

double f(double p)
{
double sum = 0;
for (int i = 0; i < n; i++)

sum += list2[i] * ((list1[i]>p) ? list1[i] - p : p - list1[i]);
return sum;
}

int main()
{
cin >> n;
for (int i = 0; i < n; i++)cin >> list1[i] >> list2[i];
double low = 0, high = 50000;
double mid, mm;
while (low + 0.000001 < high)
{
mid = (low + high) / 2;
mm = (mid + high) / 2;
if (f(mid) < f(mm))high = mm;
else low = mid;
}
cout << mid;
return 0;
}


实际上,这个问题要么有唯一解,要么有无穷解。

本题就是要找一个点,使得左边的人和右边的人一样多。

这个点可能是所给的某个点,也有可能是某2个相邻的点之间的线段上 任意一点都行。

也就是说,一定可以取所给的某个点作为答案。

但是这没什么卵用啊,那么多点,如果不排序的话就只能把每个点试一下,不管是哪种都不会很快。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: