๊ธฐ๋ก๋ฐฉ

BOJ_2304 : ์ฐฝ๊ณ  ๋‹ค๊ฐํ˜• ๋ณธ๋ฌธ

CodingTest/Java

BOJ_2304 : ์ฐฝ๊ณ  ๋‹ค๊ฐํ˜•

Soom_1n 2023. 1. 15. 01:07

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

2304๋ฒˆ: ์ฐฝ๊ณ  ๋‹ค๊ฐํ˜•

์ฒซ ์ค„์—๋Š” ๊ธฐ๋‘ฅ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 1 ์ด์ƒ 1,000 ์ดํ•˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ N ๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ค„์— ๊ฐ ๊ธฐ๋‘ฅ์˜ ์™ผ์ชฝ ๋ฉด์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ L๊ณผ ๋†’์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ H๊ฐ€ ํ•œ ๊ฐœ์˜

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ฃผ์–ด์ง„ ๊ธฐ๋‘ฅ๋“ค์„ ์‚ฌ์šฉํ•ด ์ฐฝ๊ณ ๋ฅผ ์ง€์„๋•Œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฉด์  ์ค‘ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์กฐ๊ฑด๋“ค์„ ๋งž์ถฐ์„œ ์ตœ์†Œ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    • ๊ฐ€์žฅ ๋†’์€ ๊ธฐ๋‘ฅ์„ ์ค‘์‹ฌ์œผ๋กœ ์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋‚ฎ์•„์ง€๋Š” ์ง€๋ถ•
    • ์˜ค๋ชฉํ•œ ๋ถ€๋ถ„์ด ์—†์–ด์•ผ ํ•˜๋ฏ€๋กœ ๋์—์„œ ๊ฐ€์žฅ ๋†’์€ ๊ธฐ๋‘ฅ๊นŒ์ง€ ๋‹ค๊ฐ€์˜ค๋ฉฐ, ๋” ํฐ ๊ธฐ๋‘ฅ์„ ๋งŒ๋‚ ๋•Œ๋งŒ ๋†’์ด๋ฅผ ๋†’์ž„

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int[][] pillar = new int[n][2];
		int max = 0, right = 0, mi = -1;

		for (int i = 0; i < n; i++) {
			pillar[i][0] = sc.nextInt();
			pillar[i][1] = sc.nextInt();
		}

		Arrays.sort(pillar, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return o1[0] - o2[0];
			}
		});

		for (int i = 0; i < n; i++) {
			if (max < pillar[i][1]) {
				max = pillar[i][1];
				mi = i;
			}
		}

		int answer = max;
		int start = pillar[0][0], now = pillar[0][1];

		for (int i = 1; i <= mi; i++) {
			if (pillar[i][1] >= now) {
				answer += (pillar[i][0] - start) * now;
				start = pillar[i][0];
				now = pillar[i][1];
			}
		}

		start = pillar[n - 1][0];
		now = pillar[n - 1][1];

		for (int i = n - 1; i >= mi; i--) {
			if (pillar[i][1] >= now) {
				answer += (start - pillar[i][0]) * now;
				start = pillar[i][0];
				now = pillar[i][1];
			}
		}

		System.out.println(answer);
	}
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๊ฐ ๊ธฐ๋‘ฅ์˜ ์œ„์น˜์™€ ๋†’์ด๋ฅผ ์ด์ฐจ์› ๋ฐฐ์—ด pillar์— ์ €์žฅํ•œ๋‹ค.
  • pillar๋ฅผ ์œ„์น˜ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
  • ๊ฐ€์žฅ ๋†’์€ ๊ธฐ๋‘ฅ์˜ ๋†’์ด์™€ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • ๋งจ ์•ž๋ถ€ํ„ฐ ๊ฐ€์žฅ ๋†’์€ ๊ธฐ๋‘ฅ๊นŒ์ง€ ์ฐฝ๊ณ ๋„ˆ๋น„๋ฅผ ๋”ํ•œ๋‹ค.
  • ๋งจ ๋’ค๋ถ€ํ„ฐ ๊ฐ€์žฅ ๋†’์€ ๊ธฐ๋‘ฅ๊นŒ์ง€ ์ฐฝ๊ณ  ๋„ˆ๋น„๋ฅผ ๋”ํ•œ๋‹ค.
  • ๊ฐ€์žฅ ๋†’์€ ๊ธฐ๋‘ฅ์˜ ๋„ˆ๋น„๋ฅผ ๋”ํ•œ๋‹ค.
  • ์ „์ฒด ์ฐฝ๊ณ  ๋„ˆ๋น„๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์˜ˆ์ „์— python์œผ๋กœ ํ’€์—ˆ์—ˆ๋Š”๋ฐ java๋กœ ๋‹ค์‹œ ํ’€์–ด๋ณด์•˜๋‹ค.
  • ๋ฐฐ์—ด์„ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ, ๋ฌธ์ œ ํƒœ๊ทธ์— ์žˆ๋Š”๋Œ€๋กœ ์Šคํƒ์œผ๋กœ๋„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์–ด๋ณด์ธ๋‹ค.

728x90