zoj 3386 Trick or Treat 求x轴上一点到各点的最大值中的最小值 点到线段距离 三分
2010-08-28 22:33
567 查看
Trick or Treat
--------------------------------------------------------------------------------
Time Limit: 5 Seconds Memory Limit: 65536 KB Special Judge
--------------------------------------------------------------------------------
Johnny and his friends have decided to spend Halloween night doing the usual candy collection from the households of their village. As the village is too big for a single group to collect the candy from all houses sequentially, Johnny and his friends have decided to split up so that each of them goes to a different house, collects the candy (or wreaks havoc if the residents don't give out candy), and returns to a meeting point arranged in advance.
There are n houses in the village, the positions of which can be identified with their Cartesian coordinates on the Euclidean plane. Johnny's gang is also made up of n people (including Johnny himself). They have decided to distribute the candy after everybody comes back with their booty. The houses might be far away, but Johnny's interest is in eating the candy as soon as possible.
Keeping in mind that, because of their response to the hospitality of some villagers, some children might be wanted by the local authorities, they have agreed to fix the meeting point by the river running through the village, which is the line y = 0. Note that there may be houses on both sides of the river, and some of the houses may be houseboats (y = 0). The walking speed of every child is 1 meter per second, and they can move along any direction on the plane.
At exactly midnight, each child will knock on the door of the house he has chosen, collect the candy instantaneously, and walk back along the shortest route to the meeting point. Tell Johnny at what time he will be able to start eating the candy.
Input
Each test case starts with a line indicating the number n of houses (1 ≤ n ≤ 50 000). The next n lines describe the positions of the houses; each of these lines contains two floating point numbers x and y (-200 000 ≤ x, y ≤ 200 000), the coordinates of a house in meters. All houses are at different positions.
A blank line follows each case. A line with n = 0 indicates the end of the input; do not write any output for this case.
Output
For each test case, print two numbers in a line separated by a space: the coordinate x of the meeting point on the line y = 0 that minimizes the time the last child arrives, and this time itself (measured in seconds after midnight). Your answer should be accurate to within an absolute or relative error of 10-5.
Sample Input
2
1.5 1.5
3 0
1
0 0
4
1 4
4 4
-3 3
2 4
5
4 7
-4 0
7 -6
-2 4
8 -5
0
Sample Output
1.500000000 1.500000000
0.000000000 0.000000000
1.000000000 5.000000000
3.136363636 7.136363636
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-10;
struct Point
{
double x,y;
};
double dis(Point a,Point b)
{
return sqrt(0.0+(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Point pos[60000];
int n;
double f(double x)//找n个点中的最大值
{
double _max=-800000;
Point t={x,0};
for(int i=0;i<n;i++)
{
double tn=dis(t,pos[i]);
_max=_max>tn?_max:tn;
}
return _max;
}
int main()
{
while(scanf("%d",&n)==1&&n)
{
double a=800000,b=-800000;
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&pos[i].x,&pos[i].y);
a=a<pos[i].x?a:pos[i].x;//左界
b=b>pos[i].x?b:pos[i].x;//右界
}
//三分求极值
while((b-a)>eps)
{
double deta=(b-a)/3;
double l,r;
l=a+deta,r=a+2*deta;// 1/3,2/3
//important 以下因上凸函数和下凸函数不同而不同
//点到线段的距离是下凸函数,
//此题中n个下凸函数的每点函数值取MAX后的函数仍然是下凸函数 important.
// 如果左右边界为a,b,三分点为l,r,则如果f(l)>f(r),则l左面部分可删去不用考虑,将区间减少为(l,b);
//如果f(l)<f(r),则r右面部分可删去不用考虑,将区间减少为(a,r);
//直到l和r重叠,三分结束,解为f(a)或f(b);
if(f(l)>=f(r)) a=l;
else b=r;
}
printf("%.9lf %.9lf/n",a,f(a));
}
return 0;
}
--------------------------------------------------------------------------------
Time Limit: 5 Seconds Memory Limit: 65536 KB Special Judge
--------------------------------------------------------------------------------
Johnny and his friends have decided to spend Halloween night doing the usual candy collection from the households of their village. As the village is too big for a single group to collect the candy from all houses sequentially, Johnny and his friends have decided to split up so that each of them goes to a different house, collects the candy (or wreaks havoc if the residents don't give out candy), and returns to a meeting point arranged in advance.
There are n houses in the village, the positions of which can be identified with their Cartesian coordinates on the Euclidean plane. Johnny's gang is also made up of n people (including Johnny himself). They have decided to distribute the candy after everybody comes back with their booty. The houses might be far away, but Johnny's interest is in eating the candy as soon as possible.
Keeping in mind that, because of their response to the hospitality of some villagers, some children might be wanted by the local authorities, they have agreed to fix the meeting point by the river running through the village, which is the line y = 0. Note that there may be houses on both sides of the river, and some of the houses may be houseboats (y = 0). The walking speed of every child is 1 meter per second, and they can move along any direction on the plane.
At exactly midnight, each child will knock on the door of the house he has chosen, collect the candy instantaneously, and walk back along the shortest route to the meeting point. Tell Johnny at what time he will be able to start eating the candy.
Input
Each test case starts with a line indicating the number n of houses (1 ≤ n ≤ 50 000). The next n lines describe the positions of the houses; each of these lines contains two floating point numbers x and y (-200 000 ≤ x, y ≤ 200 000), the coordinates of a house in meters. All houses are at different positions.
A blank line follows each case. A line with n = 0 indicates the end of the input; do not write any output for this case.
Output
For each test case, print two numbers in a line separated by a space: the coordinate x of the meeting point on the line y = 0 that minimizes the time the last child arrives, and this time itself (measured in seconds after midnight). Your answer should be accurate to within an absolute or relative error of 10-5.
Sample Input
2
1.5 1.5
3 0
1
0 0
4
1 4
4 4
-3 3
2 4
5
4 7
-4 0
7 -6
-2 4
8 -5
0
Sample Output
1.500000000 1.500000000
0.000000000 0.000000000
1.000000000 5.000000000
3.136363636 7.136363636
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-10;
struct Point
{
double x,y;
};
double dis(Point a,Point b)
{
return sqrt(0.0+(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Point pos[60000];
int n;
double f(double x)//找n个点中的最大值
{
double _max=-800000;
Point t={x,0};
for(int i=0;i<n;i++)
{
double tn=dis(t,pos[i]);
_max=_max>tn?_max:tn;
}
return _max;
}
int main()
{
while(scanf("%d",&n)==1&&n)
{
double a=800000,b=-800000;
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&pos[i].x,&pos[i].y);
a=a<pos[i].x?a:pos[i].x;//左界
b=b>pos[i].x?b:pos[i].x;//右界
}
//三分求极值
while((b-a)>eps)
{
double deta=(b-a)/3;
double l,r;
l=a+deta,r=a+2*deta;// 1/3,2/3
//important 以下因上凸函数和下凸函数不同而不同
//点到线段的距离是下凸函数,
//此题中n个下凸函数的每点函数值取MAX后的函数仍然是下凸函数 important.
// 如果左右边界为a,b,三分点为l,r,则如果f(l)>f(r),则l左面部分可删去不用考虑,将区间减少为(l,b);
//如果f(l)<f(r),则r右面部分可删去不用考虑,将区间减少为(a,r);
//直到l和r重叠,三分结束,解为f(a)或f(b);
if(f(l)>=f(r)) a=l;
else b=r;
}
printf("%.9lf %.9lf/n",a,f(a));
}
return 0;
}
相关文章推荐
- 【三分查找求单峰函数的最值】 ZOJ 3386 Trick or Treat
- BNU 4260 Trick or Treat && ZOJ 3386 (三分查找)
- ZOJ 3368 Trick or Treat(二分答案)
- BNUoj 4260 Trick or Treat (三分)
- BNU 4260 ——Trick or Treat——————【三分求抛物线顶点】
- 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- poj3264线段数求最大最小值
- [BZOJ1589] [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点 + 记忆化搜索)
- [USACO08DEC]Trick or Treat on the Farm
- Trick or Treat
- BZOJ 1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 tarjan+拓扑
- 3592 - Trick or Treat!
- ZOJ-1654 Place the Robots 拆行拆列构图+二分匹配 Or 最大独立点集+TLE
- 根据当前经纬度 获取一定距离范围内的两个经纬度的点值 经纬度最大最小值
- 【BZOJ1589】【USACO 2008 Dec Gold】 1.Trick or Treat on the Farm 基环树裸DP、
- bzoj千题计划161:bzoj1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】
- 洛谷P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(tarjan缩点+记忆化)
- zoj 3203 Light Bulb 三分,求凸函数的最大值
- BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果