hdu 5033(数学)

来源:互联网 发布:aes算法的原理和算法 编辑:程序博客网 时间:2024/05/15 23:45

题意:有一个人在到处是高楼大厦的地方抬头仰望,假设所有高楼都在一条数轴上,给出了高楼的位置和高度,然后给出了人的位置(高度为0.....),问人能看到的阳光的最大角度是多少。

题解:因为人的位置还会变化,纯暴力会超时,所以把高楼和人的位置都整合到一起,按位置排序,然后从左到右根据高楼之间位置和高度的比例,将可能成为最大角度的楼都放到栈里,如果是人就根据栈内的所有值得出左边最大角度的余角,然后逆序,重复操作,得到右边最大角度的余角,180-两个角度就是最终解。难点就是如何用比例筛选。

#include <stdio.h>#include <algorithm>#include <math.h>using namespace std;const int N = 200005;const double pi = acos(-1.0);struct Point {double x, h;int flag, id;double dir[2];}p[N], q[N];int n, que, cnt;bool cmp(Point a, Point b) {return a.x < b.x;}bool cmp2(Point a, Point b) {return a.id < b.id;}double solve(Point a, Point b) {double dx = fabs(b.x - a.x);double dh = b.h - a.h;return dh / dx;}void solve2(int d) {q[0] = p[0];int top = 0;for (int i = 1; i < cnt; i++) {if (!p[i].flag) {while (top && solve(p[i], q[top]) < solve(q[top], q[top - 1]))top--;q[++top] = p[i];}else {int temp = top;while (temp && solve(p[i], q[temp]) < solve(p[i], q[temp - 1]))temp--;p[i].dir[d] = solve(p[i], q[temp]);}}}int main() {int t, cas = 1;scanf("%d", &t);while (t--) {scanf("%d", &n);cnt = 0;for (int i = 0; i < n; i++) {scanf("%lf%lf", &p[cnt].x, &p[cnt].h);p[cnt].id = cnt;p[cnt++].flag = 0;}scanf("%d", &que);for (int i = 0; i < que; i++) {scanf("%lf", &p[cnt].x);p[cnt].h = 0;p[cnt].id = cnt;p[cnt++].flag = 1;}sort(p, p + cnt, cmp);solve2(0);reverse(p, p + cnt);solve2(1);sort(p, p + cnt, cmp2);printf("Case #%d:\n", cas++);for (int i = 0; i < cnt; i++) {if (p[i].flag) {double res = 180.0 - atan(p[i].dir[0]) * 180.0 / pi - atan(p[i].dir[1]) * 180.0 / pi;printf("%.10lf\n", res);}}}return 0;}


0 0
原创粉丝点击