240629 고층 건물

Jongleee·2024년 6월 29일
0

TIL

목록 보기
612/737
static int n;
static int[] buildings;
static int[] visibleCount;

public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	n = Integer.parseInt(br.readLine());
	buildings = new int[n];
	visibleCount = new int[n];

	StringTokenizer st = new StringTokenizer(br.readLine(), " ");
	for (int i = 0; i < n; i++) {
		buildings[i] = Integer.parseInt(st.nextToken());
	}

	for (int i = 0; i < n - 1; i++) {
		visibleCount[i]++;
		visibleCount[i + 1]++;
		double maxSlope = (double) buildings[i + 1] - buildings[i];

		for (int j = i + 2; j < n; j++) {
			double currentSlope = calculateSlope(i, j);
			if (currentSlope > maxSlope) {
				maxSlope = currentSlope;
				visibleCount[i]++;
				visibleCount[j]++;
			}
		}
	}

	int maxVisible = 0;
	for (int count : visibleCount) {
		if (count > maxVisible) {
			maxVisible = count;
		}
	}

	System.out.println(maxVisible);
}

static double calculateSlope(int i, int j) {
	return (double) (buildings[j] - buildings[i]) / (j - i);
}

출처:https://www.acmicpc.net/problem/1027

0개의 댓글