HDU 5033 (monotonous stack maintenance convex hull) Building

Source: Internet
Author: User
Tags define local

Question:

On the X axis, a person has tall buildings on both sides of the building. The horizontal coordinate XI of the building and the height Hi of the building are also POs, asking the person to see the maximum angle of the sky.

Analysis:

Sort the positions of buildings and people from left to right. Use the stack to find the convex hull of the building on the left of a person, find the smallest angle, and then symmetric it, find the minimum angle of the building on the right. The two angles add up to the answer.

Scan the building on the left of a person from left to right. The following two situations will go out of the stack:

  1. The element building height at the top of the stack is less than or equal to the height of the currently scanned building. Therefore, this is a monotonous stack.
  2. The straight line slope of the top two roofs of the stack is less than the slope of the roof of the stack and the straight line of the current roof (here the slope refers to the absolute value ), this ensures that the curve of all the roofs in the stack is a convex hull.

Then, when the minimum angle of sight and vertical line are requested, if the angle between sight and horizontal line of the building on the top of the stack is greater than the angle between the second building on the stack

 

The pair in STL is used in the Code. pair is equivalent to a struct with two elements, first and second. The defined small value is used to determine whether the container X is smaller than Y. This operation first judges the relationship between X. First <Y. first, and then the relationship between X. Second <Y. Second. Therefore, you must note that t1 and t2 must support the <operator. Otherwise, an error is reported during compilation.

 

Although there is no graph, if you understand it, you have a graph in your mind. ===| |

 1 //#define LOCAL 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6  7 const double pi = acos(-1.0); 8 const int maxn = 100000 + 10; 9 10 pair<double, double> p[maxn];11 pair<double, int> pos[maxn];12 double ans[maxn];13 int stack[maxn], n, Q;14 15 double slope(const int a, const int b)16 {17     return -(p[a].second - p[b].second) / (p[a].first - p[b].first);18 }19 20 double angle(const int a, const int b)21 {22     return atan((pos[b].first - p[a].first) / p[a].second);23 }24 25 void solve()26 {27     int j = 0, top = 0;28     for(int i = 0; i < Q; ++i)29     {30         while(j < n && p[j].first < pos[i].first)31         {32             while(top > 0 && p[stack[top]].second <= p[j].second)   top--;33             while(top >= 2 && slope(stack[top], j) < slope(stack[top - 1], stack[top])) top--;34             stack[++top] = j;35             j++;36         }37         while(top >= 2 && angle(stack[top], i) > angle(stack[top - 1], i))    top--;38         ans[pos[i].second] += angle(stack[top], i);39     }40 }41 42 int main()43 {44     #ifdef LOCAL45         freopen("5033in.txt", "r", stdin);46     #endif47 48     int T, kase;49     scanf("%d", &T);50     for(int kase = 1; kase <= T; ++kase)51     {52         scanf("%d", &n);53         for(int i = 0; i < n; ++i)54             scanf("%lf%lf", &p[i].first, &p[i].second);55         sort(p, p + n);56         57         scanf("%d", &Q);58         for(int i = 0; i < Q; ++i)59         {60             scanf("%lf", &pos[i].first);61             pos[i].second = i;62             ans[i] = 0.0;63         }64         sort(pos, pos + Q);65         solve();66 67         reverse(p, p + n);68         reverse(pos, pos + Q);69         for(int i = 0; i < n; ++i)  p[i].first = -p[i].first;70         for(int i = 0; i < Q; ++i)  pos[i].first = -pos[i].first;71         solve();72 73         printf("Case #%d:\n", kase);74         for(int i = 0; i < Q; ++i)75             printf("%.10lf\n", ans[i]/pi*180.0);76     }77 78     return 0;79 }
Code Jun

 

HDU 5033 (monotonous stack maintenance convex hull) 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.