HDU 5033 Building

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.