티스토리 뷰
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
문제풀이

그림과 같이 빌딩들이 지어져 있다.
이 지역에서는 왼쪽과 오른쪽으로 창문을 열었을 때, 양쪽 모두 거리 2 이상의 공간이 확보될 때 조망권이 확보된다고 말한다.
빌딩들에 대한 정보가 주어질 때 조망권이 확보된 세대의 수를 찾는 문제다.
양쪽 모두 거리 2 이상의 공간이 확보된다면 조망권이 확보되므로 2 이하의 거리들만 확인해주면 된다.
양쪽 건물들 중에서 현재 건물의 높이보다 큰 건물이 있다면 조망권 확보 실패
현재 건물보다 높이가 낮은 건물들 중 제일 높은 건물과의 높이 차이를 계산하면 조망권이 확보된 세대의 수를 찾을 수 있다!
코드
C++ 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include <vector> | |
using namespace std; | |
int main(int argc, char** argv) | |
{ | |
int T = 10; | |
for(int test_case = 1; test_case <= T; ++test_case) { | |
int n; | |
cin >> n; | |
vector<int> b(n); | |
for (int i = 0; i < n; i++) cin >> b[i]; | |
int answer = 0; | |
for (int i = 2; i < n - 2; i++) { | |
bool check = true; | |
int view = 255; | |
for (int j = -2; j <= 2; j++) { | |
if (j == 0) continue; | |
if (b[i + j] > b[i]) { | |
check = false; | |
break; | |
} | |
view = min(view, b[i] - b[i + j]); | |
} | |
if (check) { | |
answer += view; | |
} | |
} | |
cout << "#" << test_case << " " << answer << "\n"; | |
} | |
return 0; | |
} |
Java 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.util.StringTokenizer; | |
class Solution { | |
public static void main(String[] args) throws Exception { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
int T = 10; | |
for (int test_case = 1; test_case <= T; test_case++) { | |
int n = Integer.parseInt(br.readLine()); | |
int b[] = new int[n]; | |
StringTokenizer st = new StringTokenizer(br.readLine(), " "); | |
for (int i = 0; i < n; i++) { | |
b[i] = Integer.parseInt(st.nextToken()); | |
} | |
int answer = 0; | |
for (int i = 2; i < n - 2; i++) { | |
boolean check = true; | |
int view = 255; | |
for (int j = -2; j <= 2; j++) { | |
if (j == 0) continue; | |
if (b[i + j] > b[i]) { | |
check = false; | |
break; | |
} | |
view = Math.min(view, b[i] - b[i + j]); | |
} | |
if (check) { | |
answer += view; | |
} | |
} | |
System.out.println("#" + test_case + " " + answer); | |
} | |
} | |
} |
'Problem Solving > SWEA' 카테고리의 다른 글
[SWEA 1220] Magnetic C++/Java (0) | 2021.07.28 |
---|---|
[SWEA 1210] Ladder1 C++/Java (0) | 2021.07.28 |
[SWEA 1209] Sum C++/Java (0) | 2021.07.28 |
[SWEA 1208] Flatten C++/Java (0) | 2021.07.28 |
[SWEA 1204] 최빈수 구하기 C++/Java (0) | 2021.07.28 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 큐
- Two Pointer
- SWEA
- SW Expert Academy
- 브루트포스
- Kotlin
- 분할 정복
- 백준
- 정렬
- 위상 정렬
- 프로그래머스
- 투 포인터
- BFS
- 이분 탐색
- 트리
- 그래프
- 알고리즘
- 구현
- programmers
- 배열
- dfs
- 자바
- 스택
- algorithm
- C++
- Java
- 문자열
- 두 포인터
- BOJ
- 재귀
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함