uvalive 4253(暴力)

题意:有n个靶子,,是平行于x轴的高度不同的线段,给出n个d(高度)、l(左端点)、r(右端点),然后给出w,让在x轴的[0,w]这个区间内射箭,问是否存在一个位置能射中所有靶子。

题解:先按高度排序,然后在[0,w]范围内二分位置,先得到射箭到第一个靶子的左右端点的斜率,然后到第二个靶子的左右端点的斜率,比较斜率范围是否可以重叠,如果不可以就在x轴上左移或右移(改变l或r),否则就缩小斜率范围,继续和下一个靶子比较。最后所有位置都不可以就输出NO。这里用了atan2(y,x)函数,表示从起点到点(x, y)这条线和x正半轴的角度,其实和atan(y/x)效果一样,只不过可以处理x==0时的特殊情况。

#include <stdio.h>#include <math.h>#include <algorithm>using namespace std;const int N = 5005;struct Point {double l, r, d;}p[N];double w;int n;bool cmp(Point a, Point b) {return a.d < b.d;}int judge(double pos) {double l = atan2(p[0].d, p[0].l – pos);double r = atan2(p[0].d, p[0].r – pos);for (int i = 1; i < n; i++) {double temp1 = atan2(p[i].d, p[i].l – pos);double temp2 = atan2(p[i].d, p[i].r – pos);if (temp2 – l > 1e-8)return 1;//右移if (r – temp1 > 1e-8)return -1;//左移l = min(l, temp1);//缩小范围r = max(r, temp2);}return 0;}int main() {int t;scanf("%d", &t);while (t–) {scanf("%lf%d", &w, &n);for (int i = 0; i < n; i++)scanf("%lf%lf%lf", &p[i].d, &p[i].l, &p[i].r);sort(p, p + n, cmp);double l = 0, r = w;int flag = 0;while (r – l > 1e-8) {double mid = (l + r) / 2;int temp = judge(mid);if (!temp) {printf("YES\n");flag = 1;break;}else if (temp == 1)l = mid;elser = mid;}if (!flag)printf("NO\n");}return 0;}

人生不能缺乏的是雨夜--淅沥地,独自的雨夜。这样的雨夜里,

uvalive 4253(暴力)

相关文章:

你感兴趣的文章:

标签云: