sgu 114 Telecasting station
2012-10-26 10:31
288 查看
题目描述:
time limit per test: 0.5 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 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
原来又傻B地想去三分。。。不知道为什么会有这么奇怪的想法。
后来小推下最优的公式,发现一个奇妙的现象。
我们先退一步思考,假设只能区给定的点。
而且目前最优解在val = a的地方取到。 且它之前点的val和为sum1 ,后面的和为sum2 。
---------------.---------------
<--sum1-->a<--sum2-->
假设点a后某段的值更优
那么值为 f(a) + sum1*deltx+a*deltx-sum2*deltx;
就是需要 a+sum1-sum2<0;
所以就是找到某个点 sum[i]>=sum
-sum[i]
答案呼之欲出!
代码来了:
114. Telecasting station
time limit per test: 0.5 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 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
原来又傻B地想去三分。。。不知道为什么会有这么奇怪的想法。
后来小推下最优的公式,发现一个奇妙的现象。
我们先退一步思考,假设只能区给定的点。
而且目前最优解在val = a的地方取到。 且它之前点的val和为sum1 ,后面的和为sum2 。
---------------.---------------
<--sum1-->a<--sum2-->
假设点a后某段的值更优
那么值为 f(a) + sum1*deltx+a*deltx-sum2*deltx;
就是需要 a+sum1-sum2<0;
所以就是找到某个点 sum[i]>=sum
-sum[i]
答案呼之欲出!
代码来了:
#include<iostream> #include<cstring> #include<cstdio> #include<set> #include<algorithm> #include<vector> #include<cstdlib> #include<cmath> #define inf 0xfffffff #define CLR(a,b) memset((a),(b),sizeof((a))) using namespace std; int const nMax = 40000; typedef int LL; typedef pair<LL,LL> pij; double const eps=1e-5; int n; typedef pair<double ,double> Int; Int a[nMax]; double sum[nMax]; int main() { scanf("%d",&n); a[0].first=0; a[0].second=0; for(int i=1;i<=n;i++){ scanf("%lf%lf",&a[i].first,&a[i].second); } sort(a+1,a+n+1); sum[0]=0; for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i].second; int i; for(i=1;i<=n;i++)if(sum[i]>=sum -sum[i]){ break; } printf("%.5lf\n",a[i].first); return 0; }
相关文章推荐
- sgu 114 三分
- SGU 114
- SGU 114 三分 类似于 HDU 4355(4355 同样也是一个三分的题目)
- 数学 + 带权中位数 - SGU 114 Telecasting station
- SGU 114 Telecasting station
- 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 135 + 184 +117+126+114+118+127
- SGU 114 Telecasting station 单峰极值函数
- sgu 114(带权中位数)
- SGU 114 Telecasting station
- 数学 + 带权中位数 - SGU 114 Telecasting station
- SGU 114 Telecasting station(水~)
- 114和118114