一、用数组储存该位置的最高点即可(图形的连续点离散化),注意左边界及右边界的情况;
注意:无论建筑物最左边是盖到哪里,你都得从1开始输出(输入输出都是integer,所以才能离散化);
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 int main() 6 { 7 int skyline[10005] = {0}; 8 int L, H, R; 9 int rightest = 0; 10 bool space = false; 11 12 while( scanf("%d%d%d", &L, &H, &R) != EOF ) // initial 13 { 14 for(int i = L; i < R; i++) 15 if(H > skyline[i]) skyline[i] = H; 16 if(R > rightest) rightest = R; 17 } 18 19 for(int i = 1; i <= rightest; i++) // "i=rightest" is in order to print "i 0", 20 if(skyline[i-1] != skyline[i]) // the last number. 21 { 22 if(space) printf(" "); 23 space = true; 24 printf("%d %d", i, skyline[i]); 25 } 26 printf("\n"); 27 28 return 0; 29 }
时间: 2024-11-03 21:58:12