๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 2847_๊ฒŒ์ž„์„ ๋งŒ๋“  ๋™์ค€์ด

๋ฟŒ์•ผ._. 2023. 12. 27. 22:06

Silver IV

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/2847)

< ๊ฒŒ์ž„์„ ๋งŒ๋“  ๋™์ค€์ด >

 

๋ฌธ์ œ ํ’€์ด 

 

๋ ˆ๋ฒจ์ด ๋†’์•„์งˆ์ˆ˜๋ก ์ ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์•ผ ํ•˜๋ฏ€๋กœ ๋†’์€ ๋ ˆ๋ฒจ๋ถ€ํ„ฐ ๋‚ฎ์€ ๋ ˆ๋ฒจ ์ˆœ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ ์ˆ˜๊ฐ€ ์ค„์–ด๋“ค๋„๋ก ํ•œ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class _2847_ { // ๊ฒŒ์ž„์„ ๋งŒ๋“  ๋™์ค€์ด

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(bf.readLine());

		int arr[] = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(bf.readLine());
		}

		int result = 0;
		if (n > 2) {
			for (int i = n - 2; i >= 0; i--) {
				if (arr[i + 1] <= arr[i]) {
					result += arr[i] - arr[i + 1] + 1;
					arr[i] = arr[i + 1] - 1;
				}
			}
		}
		System.out.println(result);
	}
}
๋ณ€์ˆ˜)
n : ๋ ˆ๋ฒจ์˜ ์ˆ˜
arr : ๊ฐ ๋ ˆ๋ฒจ์˜ ์ ์ˆ˜
result : ๊ฐ์†Œ์‹œํ‚ค๋Š” ์ˆ˜

 

๊ฐ ๋ ˆ๋ฒจ๋งˆ๋‹ค ์ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋†’์€ ๋ ˆ๋ฒจ๋ถ€ํ„ฐ ๋‚ฎ์€ ๋ ˆ๋ฒจ์˜ ์ ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋ฉด์„œ ๋‚ฎ์€ ๋ ˆ๋ฒจ์˜ ์ ์ˆ˜๊ฐ€ ๋†’์€ ๋ ˆ๋ฒจ์˜ ์ ์ˆ˜๋ณด๋‹ค ๋‚ฎ๋„๋ก ๊ฐ’์„ ๋ฐ”๊ฟ”์ค€๋‹ค. ์ •๋‹ต์œผ๋กœ ์ ์ˆ˜๋ฅผ ๊ฐ์†Œ์‹œํ‚ค๋Š” ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.