B. The Meeting Place Cannot Be Changed
来源:互联网 发布:免费qq群发软件 编辑:程序博客网 时间:2024/06/18 12:19
The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.
At some points on the road there are n friends, and i-th of them is standing at the point xi meters and can move with any speed no greater than vi meters per second in any of the two directions along the road: south or north.
You are to compute the minimum time needed to gather all the n friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate.
The first line contains single integer n (2 ≤ n ≤ 60 000) — the number of friends.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) — the current coordinates of the friends, in meters.
The third line contains n integers v1, v2, ..., vn (1 ≤ vi ≤ 109) — the maximum speeds of the friends, in meters per second.
Print the minimum time (in seconds) needed for all the n friends to meet at some point on the road.
Your answer will be considered correct, if its absolute or relative error isn't greater than 10 - 6. Formally, let your answer be a, while jury's answer be b. Your answer will be considered correct if holds.
37 1 31 2 1
2.000000000000
45 10 3 22 3 2 4
1.400000000000
In the first sample, all friends can gather at the point 5 within 2 seconds. In order to achieve this, the first friend should go south all the time at his maximum speed, while the second and the third friends should go north at their maximum speeds.
题目很简单,二分查找,但是做了好几遍被case 3 卡死,一直tle,简直了,后来细查才发现问题。
重点是设定数组时注意运算时会出现转换导致精度和结果问题,然后就wa了,最好全用double型,简单易考虑;
代码如下:
//// main.cpp// B. The Meeting Place Cannot Be Changed//// Created by 徐智豪 on 2017/3/23.// Copyright © 2017年 徐智豪. All rights reserved.//#include <iostream>#include <cmath>#include <iomanip>using namespace std;double x[70000],v[70000];int n;bool fun( double time){ double maxleft=x[0]-v[0]*time,minright=x[0]+v[0]*time,temp1,temp2; for(int i=1;i<n;i++) { temp1=x[i]-time*v[i]; temp2=x[i]+time*v[i]; if(temp2<maxleft||temp1>minright) return false; maxleft=max(maxleft,temp1); minright=min( temp2,minright); } return true;}int main(int argc, const char * argv[]) { cin>>n; for(int i=0;i<n;i++) { cin>>x[i]; } for(int i=0;i<n;i++) { cin>>v[i]; } double tfirst=0,tlast=1e9,tmid=(tfirst+tlast)/2; while(tlast-tfirst>1e-7) { tmid=(tfirst+tlast)/2; if(fun(tmid)) { tlast=tmid; } else { tfirst=tmid; } } cout<<setprecision(12)<<fixed<<tfirst<<endl; return 0;}
- B. The Meeting Place Cannot Be Changed
- B. The Meeting Place Cannot Be Changed
- B The Meeting Place Cannot Be Changed
- B. The Meeting Place Cannot Be Changed
- B. The Meeting Place Cannot Be Changed
- B. The Meeting Place Cannot Be Changed
- B. The Meeting Place Cannot Be Changed
- B. The Meeting Place Cannot Be Changed
- 【codeforces 782B】The Meeting Place Cannot Be Changed
- cf 782B The Meeting Place Cannot Be Changed
- codeforces 782B The Meeting Place Cannot Be Changed
- Codeforces 780B-The Meeting Place Cannot Be Changed
- 【Codeforces 782 B The Meeting Place Cannot Be Changed】+ 二分
- 782B The Meeting Place Cannot Be Changed
- codeforces B. The Meeting Place Cannot Be Changed【二分】
- codeforces 782B The Meeting Place Cannot Be Changed
- B. The Meeting Place Cannot Be Changed----二分答案
- CodeForces 780B The Meeting Place Cannot Be Changed【二分】
- android:toolbar用法详解
- Winform中,坐标信息块、十字交叉线或其它控件随着鼠标移动的性能比较之小谈!
- Java集合框架综述
- 求一组数中的第K大数,采用递归方法
- 欢迎使用CSDN-markdown编辑器
- B. The Meeting Place Cannot Be Changed
- javaServlet一
- equal与==的区别
- 每日学习一个Java类:java.lang.Object
- OpenGL入门笔记
- Android酷炫实用的开源框架(UI框架)
- UILabel内边距
- ACM-显示器
- html入门(表格元素)