Question:
There are n buildings on the ground. You can stand in M positions and ask how much angle the sky can be seen at each position.
Ideas:
Obviously, we can maintain monotonicity on both sides of the station. Therefore, we can sort the positions of stations and buildings from left to right and maintain them from right to left. Here we can use the monotonous stack. if the newly scanned location is a building, the building with the stack shorter than it will be useless. If the stack is located, you can determine if you can see the building at the bottom of the stack (the highest building) so the front building will not affect the back of the stack.
Code:
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<cassert>#include<vector>#include<set>#include<map>#include<queue>using namespace std;#define N 100010#define PI acos(-1)struct node { int id; double x, h; bool operator<(const node ff) const { return x < ff.x; }} nd[N * 2], st[N];double ansl[N], ansr[N];int n, m, t, T, tot, top;int main() { int i, j, id; double fh, fx, dx, k; scanf("%d", &T); for (t = 1; t <= T; t++) { tot = 0; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%lf%lf", &fx, &fh); nd[tot].id = 0; nd[tot].x = fx; nd[tot].h = fh; tot++; } scanf("%d", &m); for (i = 1; i <= m; i++) { scanf("%lf", &fx); nd[tot].id = i; nd[tot].x = fx; tot++; ansl[i] = ansr[i] = 0; } sort(nd, nd + tot); //L top = 0; for (i = 0; i < tot; i++) { if (nd[i].id) { id = nd[i].id; dx = nd[i].x; fh = st[0].h; fx = st[0].x; while (top > 1 && fh / (dx - fx) >= st[top - 1].h / (dx - st[top - 1].x)) top--; for (j = 0; j < top; j++) { k = st[j].h / (dx - st[j].x); if (ansl[id] < k) ansl[id] = k; } } else { while (top && nd[i].h >= st[top - 1].h) top--; st[top++] = nd[i]; } } //R top = 0; for (i = tot - 1; i >= 0; i--) { if (nd[i].id) { id = nd[i].id; dx = nd[i].x; fh = st[0].h; fx = st[0].x; while (top > 1 && fh / (fx - dx) >= st[top - 1].h / (st[top - 1].x - dx)) top--; for (j = 0; j < top; j++) { k = st[j].h / (st[j].x - dx); if (ansr[id] < k) ansr[id] = k; } } else { while (top && nd[i].h >= st[top - 1].h) top--; st[top++] = nd[i]; } } printf("Case #%d:\n", t); for (i = 1; i <= m; i++) { printf("%f\n", 180.0 - (atan(ansl[i]) + atan(ansr[i])) / PI * 180); } } return 0;}
HDU 5033 Building