Skip to content

Commit ed5fa22

Browse files
committed
[Gold IV] Title: 고층 건물, Time: 100 ms, Memory: 9396 KB -BaekjoonHub
1 parent a2c8b4e commit ed5fa22

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Gold IV] 고층 건물 - 1027
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1027)
4+
5+
### 성능 요약
6+
7+
메모리: 9396 KB, 시간: 100 ms
8+
9+
### 분류
10+
11+
수학, 브루트포스 알고리즘, 기하학
12+
13+
### 제출 일자
14+
15+
2026년 4월 27일 21:44:42
16+
17+
### 문제 설명
18+
19+
<p>세준시에는 고층 빌딩이 많다. 세준시의 서민 김지민은 가장 많은 고층 빌딩이 보이는 고층 빌딩을 찾으려고 한다. 빌딩은 총 N개가 있는데, 빌딩은 선분으로 나타낸다. i번째 빌딩 (1부터 시작)은 (i,0)부터 (i,높이)의 선분으로 나타낼 수 있다. 고층 빌딩 A에서 다른 고층 빌딩 B가 볼 수 있는 빌딩이 되려면, 두 지붕을 잇는 선분이 A와 B를 제외한 다른 고층 빌딩을 지나거나 접하지 않아야 한다. 가장 많은 고층 빌딩이 보이는 빌딩을 구하고, 거기서 보이는 빌딩의 수를 출력하는 프로그램을 작성하시오.</p>
20+
21+
### 입력
22+
23+
<p>첫째 줄에 빌딩의 수 N이 주어진다. N은 50보다 작거나 같은 자연수이다. 둘째 줄에 1번 빌딩부터 그 높이가 주어진다. 높이는 1,000,000,000보다 작거나 같은 자연수이다.</p>
24+
25+
### 출력
26+
27+
<p>첫째 줄에 문제의 정답을 출력한다.</p>
28+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
3+
const { captureRejectionSymbol } = require("events");
4+
const fs = require("fs");
5+
6+
const input = fs
7+
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
8+
.toString()
9+
.trim()
10+
.split("\n");
11+
12+
const N = Number(input[0]);
13+
const heights = input[1].split(" ").map(Number);
14+
15+
/**
16+
* 오른쪽 건물이 보이려면
17+
* : A와 B 잇는 직선 기울기가 A, B 사이 모든 건물들과 A를 잇는 직선 기울기보다 커야함
18+
*
19+
* 왼쪽 건물이 보이려면
20+
* : 위와 반대로 작아야함
21+
*/
22+
23+
let max = 0;
24+
25+
for (let i = 0; i < N; i++) {
26+
let count = 0;
27+
let left = Infinity;
28+
let right = -Infinity;
29+
30+
//왼쪽
31+
for (let j = i - 1; j >= 0; j--) {
32+
const slope = (heights[i] - heights[j]) / (i - j);
33+
if (j === i - 1 || slope < left) {
34+
left = slope;
35+
count++;
36+
}
37+
}
38+
39+
//오른쪽
40+
for (let j = i + 1; j < N; j++) {
41+
const slope = (heights[j] - heights[i]) / (j - i);
42+
if (j === i + 1 || slope > right) {
43+
right = slope;
44+
count++;
45+
}
46+
}
47+
max = Math.max(max, count);
48+
}
49+
50+
console.log(max);

0 commit comments

Comments
 (0)