๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[SW Expert Academy] - (D3)5789. ํ˜„์ฃผ์˜ ์ƒ์ž ๋ฐ”๊พธ๊ธฐ

by ์ฃผ๋ฐœ2 2020. 6. 5.
๋ฐ˜์‘ํ˜•

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWYygN36Qn8DFAVm&categoryId=AWYygN36Qn8DFAVm&categoryType=CODE

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

์ฝ”๋“œ

import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int T = scan.nextInt();
		for(int tc=1; tc<=T; tc++) {
			int N = scan.nextInt();	// ์ƒ์ž ๊ฐฏ์ˆ˜
			int Q = scan.nextInt();	// ๋ฐ˜๋ณตํ•  ํšŸ์ˆ˜
			int[] boxes = new int[N];
			
			// Q๋ฒˆ๋™์•ˆ ๋ฐ˜๋ณตํ•ด์„œ ์ง„ํ–‰
			for(int i=1; i<=Q; i++) {
				int L = scan.nextInt();
				int R = scan.nextInt();
				
				/*
				 *  L๋ฒˆ ~ R๋ฒˆ ์ƒ์ž -> i๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ
				 *  ๋ฐฐ์—ด์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ -1ํ•ด์ค€๋‹ค.
				 */
				for(int j=L-1; j<=R-1; j++) {
					boxes[j] = i;
				}
			}
			
			System.out.print("#" + tc + " ");
			for(int i=0; i<N; i++) {
				System.out.print(boxes[i] + " ");
			}
			System.out.println();
		}
		scan.close();
	}

}

ํ’€์ด

๋ฌธ์ œ์— ์ฃผ์–ด์ง„๋Œ€๋กœ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

๋ฌธ์ œ์˜ ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

 - 1 ~ N๋ฒˆ๊นŒ์ง€ N๊ฐœ์˜ ์ƒ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ดˆ๊ธฐ๊ฐ’: 0

 - QํšŒ๋™์•ˆ ๋™์ผํ•œ ์ˆซ์ž๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

    - i (1 <= i <= Q)๋ฒˆ์งธ ์ž‘์—…์— ๋Œ€ํ•ด L๋ฒˆ ~ R๋ฒˆ ์ƒ์ž๊นŒ์ง€์˜ ๊ฐ’์„ i๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

 - QํšŒ๋™์•ˆ ์ž‘์—…ํ•œ ํ›„ ์ƒ์ž์˜ ๊ฐ’๋“ค์„ ์ถœ๋ ฅํ•œ๋‹ค.

   * L=1, R=3 ์ผ๋•Œ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ, L=0 ~ R=2๊นŒ์ง€๋กœ ํŒ๋‹จํ•ด์•ผ ํ•œ๋‹ค. (-1 ํ•ด์คŒ)

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€