zoukankan      html  css  js  c++  java
  • hdu 5033 Building (单调栈 或 暴力枚举 )

    Description

    Once upon a time Matt went to a small town. The town was so small and narrow that he can regard the town as a pivot. There were some skyscrapers in the town, each located at position x i with its height h i. All skyscrapers located in different place. The skyscrapers had no width, to make it simple. As the skyscrapers were so high, Matt could hardly see the sky.Given the position Matt was at, he wanted to know how large the angle range was where he could see the sky. Assume that Matt's height is 0. It's guaranteed that for each query, there is at least one building on both Matt's left and right, and no building locate at his position.
     

    Input

    The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow. 
    
    Each test case begins with a number N(1<=N<=10^5), the number of buildings. 
    
    In the following N lines, each line contains two numbers, x i(1<=x i<=10^7) and h i(1<=h i<=10^7). 
    
    After that, there's a number Q(1<=Q<=10^5) for the number of queries. 
    
    In the following Q lines, each line contains one number q i, which is the position Matt was at.
     

    Output

    For each test case, first output one line "Case #x:", where x is the case number (starting from 1). 
    
    Then for each query, you should output the angle range Matt could see the sky in degrees. The relative error of the answer should be no more than 10^(-4).
     

    Sample Input

    3
    3
    1 2
    2 1
    5 1
    1
    4
    3
    1 3
    2 2
    5 1
    1
    4
    3
    1 4
    2 3
    5 1
    1
    4
     

    Sample Output

    Case #1:
    101.3099324740
    Case #2:
    90.0000000000
    Case #3:
    78.6900675260

    第一种方法是用单调栈维护

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<stdlib.h>
     7 using namespace std;
     8 #define N 200006
     9 #define PI acos(-1.0)
    10 int n,m;
    11 struct Node{
    12     double x,h;
    13     int id;
    14     double angle1;
    15     double angle2;
    16     bool vis;
    17 }a[N],q[N];
    18 bool cmp1(Node a,Node b){
    19     return a.x<b.x;
    20 }
    21 bool cmp2(Node a,Node b){
    22     return a.id<b.id;
    23 }
    24 double xieLv(Node a,Node b){
    25     double w1=fabs(b.x-a.x);
    26     double w2=b.h-a.h;
    27 
    28     return w2/w1;
    29 }
    30 int main()
    31 {
    32     int ac=0;
    33     int t;
    34     scanf("%d",&t);
    35     while(t--){
    36       scanf("%d",&n);
    37       for(int i=0;i<n;i++){
    38           scanf("%lf%lf",&a[i].x,&a[i].h);
    39           a[i].id=i;
    40           a[i].vis=false;
    41       }
    42       scanf("%d",&m);
    43       for(int i=n;i<n+m;i++){
    44           scanf("%lf",&a[i].x);
    45           a[i].h=0;
    46           a[i].vis=true;
    47           a[i].id=i;
    48       }
    49       n+=m;
    50       sort(a,a+n,cmp1);
    51 
    52       q[0]=a[0];
    53       int top=0;
    54       for(int i=1;i<n;i++){
    55         if(a[i].vis==false){
    56             while(top && xieLv(a[i],q[top])<xieLv(q[top],q[top-1]))
    57             top--;
    58             q[++top]=a[i];
    59         }
    60         else{
    61             int tmp=top;
    62             while(tmp && xieLv(a[i],q[tmp])<xieLv(a[i],q[tmp-1]))
    63                 tmp--;
    64             a[i].angle1=xieLv(a[i],q[tmp]);
    65 
    66         }
    67       }
    68 
    69       q[0]=a[n-1];
    70       top=0;
    71       for(int i=n-2;i>=0;i--){
    72         if(a[i].vis==false){
    73             while(top && xieLv(a[i],q[top])<xieLv(q[top],q[top-1]))
    74             top--;
    75             q[++top]=a[i];
    76         }
    77         else{
    78             int tmp=top;
    79             while(tmp && xieLv(a[i],q[tmp])<xieLv(a[i],q[tmp-1]))
    80                 tmp--;
    81             a[i].angle2=xieLv(a[i],q[tmp]);
    82 
    83         }
    84       }
    85 
    86       sort(a,a+n,cmp2);
    87       printf("Case #%d
    ",++ac);
    88       for(int i=0;i<n;i++){
    89           if(a[i].vis){
    90              double ans=PI-atan(a[i].angle1)-atan(a[i].angle2);
    91              printf("%.10lf
    ",ans*180/PI);
    92           }
    93       }
    94 
    95     }
    96     return 0;
    97 }
    View Code
     第二种方法是先预处理,再二分查找
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<stdlib.h>
     7 using namespace std;
     8 #define N 500006
     9 #define PI acos(-1.0)
    10 int n,m;
    11 struct Node{
    12     double x,h;
    13 }a[N];
    14 int L[N];
    15 int R[N];
    16 double b[N];
    17 bool cmp1(Node a,Node b){
    18     return a.x<b.x;
    19 }
    20 int main()
    21 {
    22     int ac=0;
    23     int t;
    24     scanf("%d",&t);
    25     while(t--){
    26         scanf("%d",&n);
    27         for(int i=0;i<n;i++){
    28             scanf("%lf%lf",&a[i].x,&a[i].h);
    29         }
    30         sort(a,a+n,cmp1);
    31         memset(L,-1,sizeof(L));
    32         memset(R,-1,sizeof(R));
    33         for(int i=0;i<n;i++){
    34             for(int j=i-1;j>=0;j--){
    35                 if(a[j].h>a[i].h){
    36                     L[i]=j;
    37                     break;
    38                 }
    39             }
    40             for(int j=i+1;j<n;j++){
    41                 if(a[i].h<a[j].h){
    42                     R[i]=j;
    43                     break;
    44                 }
    45             }
    46         }
    47         for(int i=0;i<n;i++){
    48             b[i]=a[i].x;
    49         }
    50 
    51         scanf("%d",&m);
    52         printf("Case #%d:
    ",++ac);
    53         for(int i=0;i<m;i++){
    54             double x;
    55             scanf("%lf",&x);
    56             int index=lower_bound(b,b+n,x)-b;
    57             int you=index;
    58             double angle1=0;
    59             double angle2=0;
    60             while(R[you]!=-1){
    61                 double w=a[you].h/(a[you].x-x);
    62                 if(w>angle1){
    63                     angle1=w;
    64                 }
    65                 you=R[you];
    66             }
    67             double w=a[you].h/(a[you].x-x);
    68             if(w>angle1){
    69                     angle1=w;
    70             }
    71 
    72             int zuo=index-1;
    73             while(L[zuo]!=-1){
    74                 double w=a[zuo].h/(x-a[zuo].x);
    75                 if(w>angle2){
    76                     angle2=w;
    77                 }
    78                 zuo=L[zuo];
    79             }
    80             w=a[zuo].h/(x-a[zuo].x);
    81             if(w>angle2){
    82                 angle2=w;
    83             }
    84 
    85             double ans=PI-atan(angle1)-atan(angle2);
    86            
    87             printf("%.10lf
    ",ans*180/PI);
    88         }
    89 
    90     }
    91     return 0;
    92 }
    View Code
  • 相关阅读:
    pgloader 学习(七) 从归档文件加载数据
    pgloader 学习(六) 加载csv 数据
    pgloader 学习(五)pgloader 参考手册
    pgloader 学习(四)一些简单操作例子
    pgloader 学习(三)快速使用
    pgloader 学习(二)特性矩阵&&命令行
    pgloader 学习(一)支持的特性
    使用readthedocs 发布 sphinx doc文档
    pgloader 方便的数据迁移工具
    circus && web comsole docker-compose 独立部署web console 的一个bug
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4789668.html
Copyright © 2011-2022 走看看