The Meeting Place Cannot Be Changed(二分+eps精度)
题目链接:http://codeforces.com/contest/782/problem/B
大致题意:给你一组小朋友的当前坐标和最大速度,可以在最大速度以内任何速度前进(其实就是要按最大速度算。。),问你要使他们达到同一坐标的最小时间。
#include<iostream> #include<cstdio> #include<math.h> #include<string.h> #include<algorithm> using namespace std; const int MAX = 60005; const double eps = 1e-6; int n; struct coor { int x, v; bool operator <(const coor &a) const { return x < a.x; } }C[MAX]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> C[i].x; for (int i = 0; i < n; i++) cin >> C[i].v; sort(C, C + n); double left = (double)C[0].x,right = (double)C[n - 1].x; while (fabs(right - left) >= eps) { double mid = (right + left) / 2; double lmax = 0, rmax = 0; for (int i = 0; i < n; i++) { if (C[i].x < mid) { lmax = max(lmax, fabs(mid - (double)C[i].x) / C[i].v); } else { rmax = max(rmax, fabs((double)C[i].x - mid) / C[i].v); } } if (lmax < rmax) { left = mid; } else { right = mid; } } double mid = fabs(left + right) / 2; double ans = 0.0; for (int i = 0; i < n; i++) { ans = max(ans, fabs((double)C[i].x - mid) / C[i].v); } printf("%f\n", ans); return 0; }
昨天晚上打的CF,感觉心态都被打崩了,不过也学到了好多知识,eps还是第一次用。。。。(以前只是见过)。
感觉对二分也认识的更深了一些,人总是在被虐中成长的嘛,给自己一点鼓励。。