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
Sample Output
用三分来做是可以的。
因为函数f一定是凹函数。
代码:
实际上,这个问题要么有唯一解,要么有无穷解。
本题就是要找一个点,使得左边的人和右边的人一样多。
这个点可能是所给的某个点,也有可能是某2个相邻的点之间的线段上 任意一点都行。
也就是说,一定可以取所给的某个点作为答案。
但是这没什么卵用啊,那么多点,如果不排序的话就只能把每个点试一下,不管是哪种都不会很快。
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个相邻的点之间的线段上 任意一点都行。
也就是说,一定可以取所给的某个点作为答案。
但是这没什么卵用啊,那么多点,如果不排序的话就只能把每个点试一下,不管是哪种都不会很快。
相关文章推荐
- SGU 114 Telecasting station(二分)
- SGU 114 Telecasting station
- sgu-114-Telecasting station
- Sgu 114 Telecasting station
- Telecasting station - SGU 114(带劝中位数)
- SGU 114
- SGU 114 Telecasting station
- sgu 114 Telecasting station
- SGU 114 Telecasting station 单峰极值函数
- sgu 135 + 184 +117+126+114+118+127
- 数学 + 带权中位数 - SGU 114 Telecasting station
- sgu 114(带权中位数)
- SGU 114 Telecasting station
- SGU 114 Telecasting station(水~)
- sgu 114 三分
- SGU 114
- SGU 114 三分 类似于 HDU 4355(4355 同样也是一个三分的题目)
- 数学 + 带权中位数 - SGU 114 Telecasting station
- 如何禁止电信114网页的DNS劫持
- [SGU 286]Ancient decoration(二分图匹配)