hdu5033 最大仰望角
生活随笔
收集整理的這篇文章主要介紹了
hdu5033 最大仰望角
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
? ? ? 給你n個樓房排成一條直線,樓房可以看成是寬度為1的線段,然后給你m組詢問,每組詢問給你一個坐標,輸出在當前坐標仰望天空的可視角度。
思路:
? ? ? 給你n個樓房排成一條直線,樓房可以看成是寬度為1的線段,然后給你m組詢問,每組詢問給你一個坐標,輸出在當前坐標仰望天空的可視角度。
思路:
? ? ? n比較大,O(n*m)肯定跪,其實我們可以優化掉凹形的時候,比如當前詢問點為x,對于右側,往右跑的時候,我們只跑升序的就行了,這樣我們只要開一個數組記錄當前點最近的右側的上升點就行了,到達當前點的時候,如果不滿足,可以直接跳到記錄的那個點上去,比賽的時候沒敢敲,感覺時間根本過不去,后來聽說可以,我又重新敲了一下,結果AC了,感覺應該是隨機數據的原因,也就是根本達不到O(n*m).還有,找小標的時候可以用二分去找,剛才寫的時候腦袋一熱突然就用容器去弄的,就是開了一個set和一個map,一個找值一個哈希值(不建議這樣寫,二分就行了,還省時間)。具體看代碼。
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<set> #include<map> using namespace std;typedef struct {double X ,Y; }Point;typedef struct {double x ,h; }NODE;NODE node[110000]; int merl[110000]; int merr[110000];bool camp(NODE a ,NODE b) {return a.x < b.x; }int main () {int t ,i ,j ,n ,m ,cas = 1;double x;scanf("%d" ,&t);while(t--){scanf("%d" ,&n);set<double>my_st;map<double ,int>mark;my_st.clear();mark.clear();for(i = 1 ;i <= n ;i ++){scanf("%lf %lf" ,&node[i].x ,&node[i].h);merl[i] = merr[i] = i;}sort(node + 1 ,node + n + 1 ,camp);for(i = 1 ;i <= n ;i ++){my_st.insert(node[i].x);mark[node[i].x] = i;}for(i = 1 ;i <= n ;i ++){for(j = i - 1 ;j >= 1 ;j --){if(node[j].h > node[i].h){merl[i] = j; break;}if(j == merl[j]) break;}}for(i = n ;i >= 1 ;i --){for(j = i + 1 ;j <= n ;j ++){if(node[j].h > node[i].h){merr[i] = j;break;}if(j == merr[j]) break;}}scanf("%d" ,&m);printf("Case #%d:\n" ,cas ++);while(m--){scanf("%lf" ,&x);int r = mark[*my_st.lower_bound(x)];int l = r - 1;double max = node[r].h * 1.0 / (node[r].x - x);int idr = r;while(merr[r] != r){r = merr[r];if(max < node[r].h * 1.0 / (node[r].x - x)){max = node[r].h * 1.0 / (node[r].x - x);idr = r;}}max = node[l].h * 1.0 / (x - node[l].x);int idl = l;while(merl[l] != l){ l = merl[l];if(max < node[l].h * 1.0 / (x - node[l].x)){max = node[l].h * 1.0 / (x - node[l].x);idl = l;}} Point A ,B ,C;A.X = node[idl].x ,A.Y = node[idl].h;B.X = x ,B.Y = 0;C.X = node[idr].x ,C.Y = node[idr].h;double x1 = A.X - x ,y1 = A.Y;double x2 = C.X - x ,y2 = C.Y;double Ang = ((x1 * x2) + (y1 * y2)) / (sqrt(x1 * x1 + y1 * y1) * sqrt(x2 * x2 + y2 * y2));Ang = acos(Ang);printf("%.10lf\n" ,Ang * 180.0 / acos(-1.0));}}return 0; }
總結
以上是生活随笔為你收集整理的hdu5033 最大仰望角的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu4370 比较抽象的最短路
- 下一篇: hdu5025 状态压缩广搜