알고리즘(BOJ)/Gold
백준 - 고층건물 1027
ch.0
2023. 2. 4. 18:45
문제풀이
기울기 : (고정건물 - 가변건물) / 거리
기울기가 바로 옆건물보다 커지는것을 체크한다.
왼쪽
바로옆인 -2
-2.5 는 -2보다 작으니까 패스
이렇게 왼쪽, 오른쪽으로 구분지어서 카운팅 해줬다.
package test;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int repeat = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
double[] array = new double[repeat];
for (int i = 0; i < repeat; i++) {
array[i] = (double) Integer.parseInt(st.nextToken());
}
double point = 0;
double L_max = -1000000001, R_max = -1000000001;
double L_angle = 0, R_angle = 0;
int count = 0, result = 0;
for (int i = 0; i < repeat; i++) {
point = array[i];
for (int j = 1; j < repeat; j++) {
if (i > i-j && i - j >= 0) {
L_angle = (array[i - j] - array[i]) / j;
if (L_max < L_angle) {
count++;
L_max = L_angle;
}
}
if (i < i+j && i + j < repeat) {
R_angle = (array[i + j] - array[i]) / j;
if (R_max < R_angle) {
count++;
R_max = R_angle;
}
}
}
if (result < count) {
result = count;
}
L_max = -1000000001;
R_max = -1000000001;
L_angle = 0;
R_angle = 0;
count = 0;
}
System.out.println(result);
}
}
* 기울기가 소수점도 있는데 처음에 int로 했었다.
* int to double 인 (double)이 생각 안나서 검색했다.
* 기준건물에서 멀어져야 하는데 그냥 첫번째 건물부터 계산하도록 했었다.