bnuoj Trick or Treat(三分查找)
来源:互联网 发布:matlab 矩阵类型 编辑:程序博客网 时间:2024/05/13 15:57
Trick or Treat
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
21.5 1.53 010 041 44 4-3 32 454 7-4 07 -6-2 48 -50
Sample Output
1.500000000 1.5000000000.000000000 0.0000000001.000000000 5.0000000003.136363636 7.136363636
题意从x轴上选择一点 使得给出的各点到改点的最大值最小,时间给的够长可以暴力 但是从样例中可以发现 各点到x轴上的一点的距离随着x轴上一点的移动而发生变化 可以观察出 距离函数并非为单调函数 即可以用三分搜索
code:
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<math.h>using namespace std;const double esp=1e-5;//精度控制在1e-5 -- 1e-10之间 否则 不是WA 就是TLE!!!试过struct node{double x,y;}p[50001];int n;double dis(node a,node b){ return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));}double solve(double pos){ double maxx=-200000; node po; po.x=pos,po.y=0; for(int i=0;i<n;i++) { double tt=dis(po,p[i]); maxx=max(maxx,tt); } return maxx;}int main(){ double l,r,ml,mr; int i,j; while(scanf("%d",&n),n) { ml=200000;mr=-200000; for(i=0;i<n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); if(ml>p[i].x) ml=p[i].x; if(mr<p[i].x) mr=p[i].x; } while((mr-ml)>esp) { double mid=(mr-ml)/3; l=ml+mid,r=ml+2*mid;// printf("%lf %lf\n",ml,mr);// printf("l=%lf solve(l)=%lf r=%lf solve(r)=%lf\n",l,solve(l),r,solve(r)); if(solve(l)>=solve(r))ml=l; else mr=r; } printf("%.9lf %.9lf\n",mr,solve(mr)); } return 0;}
- bnuoj Trick or Treat(三分查找)
- BNUoj 4260 Trick or Treat (三分)
- ZOJ Trick or Treat //三分查找
- BNU 4260 Trick or Treat && ZOJ 3386 (三分查找)
- HDU4071Trick or Treat(三分查找)
- 【三分查找求单峰函数的最值】 ZOJ 3386 Trick or Treat
- zoj 3386 Trick or Treat 三分 求最大值的 最小值
- 3592 - Trick or Treat!
- POJ3873 Trick or Treat
- Trick or Treat
- PKU Trick or Treat
- HDU4071 Trick or Treat 二分
- ZOJ 3368 Trick or Treat(二分答案)
- [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点+记忆化搜索)
- 【BZOJ1589】【USACO 2008 Dec Gold】 1.Trick or Treat on the Farm 基环树裸DP、
- 洛谷P2921/BZOJ1589[USACO08DEC]在农场万圣节Trick or Treat on the Farm
- BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- 二维数组
- vxworks高精度延时
- 偶然发现不占“位置”的三个字节组合
- 汉字转拼音的导致的内存溢出
- 字符串数组
- bnuoj Trick or Treat(三分查找)
- 黑马程序员—网络编程(二)
- 股票 MACD指标的买入形态图解
- 【正则】个人的一些正则写法 欢迎验证
- Word使用技巧
- 遵从自己的内心就永远不会迷失前进的方向
- 黑马程序员__流程控制
- SpringMVC 拦截JS导致Jquery无法识别问题的解决方案
- 程序在内存中运行的奥秘