hdu 5033(数学)

题意:有一个人在到处是高楼大厦的地方抬头仰望,假设所有高楼都在一条数轴上,给出了高楼的位置和高度,然后给出了人的位置(高度为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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值