UVa Problem 105 – Skyline Problem

來源:互聯網
上載者:User
// UVa Problem 105 - Skyline Problem// Verdict: Accepted// Submission Date: 2011-11-22// UVa Run Time: 0.036s//// 著作權(C)2011,邱秋。metaphysis # yeah dot net//// [解題方法]// 最初是想把交點都求出來,然後判斷交點是否在矩形內,但是寫出來的代碼醜陋難看,心想,難道沒有更好// 的方法嗎?仔細觀察了一下題目給的圖,靈機一動,難道不能用數組來類比高樓嗎?反正座標最大也就是// 10000,使用一維數組來表示橫座標,數組的值來表示縱座標,那麼矩形的 “並” 就相當容易表示了,只要// 判斷在某橫座標位置最大的縱座標即可,可以使用類似於填充法的思想來建立 “高樓” 數組,對於一個給定// 的矩形 (L,H,R),將數組下標為 [L, R] 範圍內的值全部置為 H,當然前提條件是數組的先前值小於// H,這就相當於實現了更高的樓將較矮的樓覆蓋的效果,從而方便的表示了矩形的 “並集”,那麼再按要求輸// 出座標值就不難了。#include <iostream>#include <cstring>using namespace std;#define MAXN 10010int grid[MAXN];int main (int argc, char const* argv[]){int left, height, right, leftMost, rightMost = 0;bool leftSetted = false;memset(grid, 0, sizeof(grid));while (cin >> left >> height >> right){for (int i = left; i <= right; i++)grid[i] = max(grid[i], height);if (!leftSetted){leftMost = left;leftSetted = true;}rightMost = max(rightMost, right);}cout << leftMost << " " << grid[leftMost];int current = leftMost;for (int i = leftMost; i <= rightMost; i++){if (grid[i] == grid[current])continue;else{if (grid[i] > grid[current])cout << " " << i << " " << grid[i];elsecout << " " << (i - 1) << " " << grid[i];current = i;}}cout << " " << rightMost << " 0\n";return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.