hdu 5033 Building 单调栈

来源:互联网 发布:vb中阶乘 编辑:程序博客网 时间:2024/06/05 18:17

第一次写的单调栈。

本题参考http://www.cnblogs.com/yuiffy/p/3984776.html

题意:

在水平的轴上,有着多个建筑物,建筑物宽度可以看做为0,高度为hi,让你求出在所给的点,所能仰望的最大角度。

AC代码:

(代码长度应该可以优化一下)

/* **********************************************Created Time: 2014-9-24 13:32:33File Name   : hdu5033.cpp*********************************************** *///#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <fstream>#include <cstring>#include <climits>#include <deque>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <utility>#include <sstream>#include <complex>#include <string>#include <vector>#include <cstdio>#include <bitset>#include <functional>#include <algorithm>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int MAXN = 1e5+5;const double PI = acos(-1.0);struct Point{        double x, h;        double k, res;        int rank;}bq[2*MAXN];int n, q;double recl[MAXN], recr[MAXN];          //record k;bool cmp(Point a, Point b){        return a.x < b.x;}bool cmp2(Point a, Point b){        return a.rank < b.rank;}void solve(){        sort(bq, bq+n+q, cmp);        int nq = n+q;        //left to right, find the left max view;        stack <Point> sta;        int cot = 0;        for(int i = 0;i < nq; i++)        {                if(bq[i].h != 0)        //building                {                        //check high                        while(!sta.empty())                        {                                if(sta.top().h <= bq[i].h)      sta.pop();                                else    break;                        }                        //                        if(sta.size() == 1)                        {                                Point &e = sta.top();                                bq[i].k = (e.h - bq[i].h)/(e.x - bq[i].x);                                sta.push(bq[i]);                        }                        else if(sta.size() == 0)                        {                                bq[i].k = 0;                                sta.push(bq[i]);                        }                        else                        {                                double curk;                                while(!sta.empty())                                {                                        Point &e = sta.top();                                        curk = (e.h-bq[i].h)/(e.x-bq[i].x);                                        if(curk >= e.k)                                                sta.pop();                                        else                                                break;                                }                                bq[i].k = curk;                                sta.push(bq[i]);                        }                }                else                    //Query                {                        double curk;                        while(!sta.empty())                        {                                Point &e = sta.top();                                curk = (e.h-bq[i].h)/(e.x-bq[i].x);                                if(curk <= e.k)                                        break;                                else                                        sta.pop();                        }                        recl[cot++] = curk;                }        }        //right to left, find the right max view;        int cot2 = cot-1;        while(!sta.empty())     sta.pop();        for(int i = nq-1; i >= 0; i--)        {                if(bq[i].h != 0)                {                        while(!sta.empty())                        {                                if(bq[i].h >= sta.top().h)       sta.pop();                                else            break;                        }                        if(sta.size() == 1)                        {                                Point &e = sta.top();                                bq[i].k = (e.h-bq[i].h)/(e.x-bq[i].x);                                sta.push(bq[i]);                        }                        else if(sta.size() == 0)                        {                                bq[i].k = 0;                                sta.push(bq[i]);                        }                        else                        {                                double curk;                                while(!sta.empty())                                {                                        Point &e = sta.top();                                        curk = (e.h-bq[i].h)/(e.x-bq[i].x);                                        if(curk <= e.k)                                                sta.pop();                                        else                                                break;                                }                                bq[i].k = curk;                                sta.push(bq[i]);                        }                }                else                {                        double curk;                        while(!sta.empty())                        {                                Point &e = sta.top();                                curk = (e.h-bq[i].h)/(e.x-bq[i].x);                                if(curk > e.k)                                        break;                                else                                        sta.pop();                        }                        recr[cot2--] = curk;                }        }}int main(){        int T, cas = 0;        scanf("%d", &T);        while(T--)        {                //input                scanf("%d", &n);                for(int i = 0;i < n; i++)                {                        scanf("%lf%lf", &bq[i].x, &bq[i].h);                        bq[i].rank = i;                }                scanf("%d", &q);                for(int i = n;i < q+n; i++)                {                        scanf("%lf", &bq[i].x);                        bq[i].h = 0, bq[i].rank = i;                }                //                //                solve();                int cot = 0;                for(int i = 0;i < q+n; i++)                {                        if(bq[i].h != 0)        continue;                        bq[i].res = 180 - atan(abs(recl[cot]))*180/PI - atan(recr[cot])*180/PI;                        cot++;                }                sort(bq, bq+n+q, cmp2);                printf("Case #%d:\n", ++cas);                for(int i = n;i < q+n; i++)                        printf("%.5lf\n", bq[i].res);        }        return 0;}


0 0
原创粉丝点击