[BaekJoon] 1092 ๋ฐฐ (Java)

SeongWon Ohยท2021๋…„ 10์›” 15์ผ
0
post-thumbnail

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/2012


๐Ÿ“ ๋ฌธ์ œํ’€์ด ๋ฐฉ๋ฒ•

๊ฐ๊ฐ ๋ฌด๊ฒŒ ์ œํ•œ์ด ์žˆ๋Š” N๊ฐœ์˜ ํฌ๋ ˆ์ธ์œผ๋กœ M๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์˜ฎ๊ธธ ๋•Œ ์‹œ๊ฐ„์ด ์–ผ๋งˆ๋‚˜ ๊ฑธ๋ฆฌ๋Š”์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ปจํ…Œ์ด๋„ˆ์™€ ํฌ๋ ˆ์ธ์„ ๊ฐ๊ฐ ์ •๋ ฌํ•œ ํ›„ ๊ฐ๊ฐ์˜ ํฌ๋ ˆ์ธ๋“ค์ด ํ˜„์žฌ ๋‚จ์•„์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ ์ค‘์—์„œ ์ž์‹ ์ด ๋“ค์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์ปจํ…Œ์ด๋„ˆ๋“ค์„ ์˜ฎ๊ธฐ๋„๋ก ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด ๋œ๋‹ค.

๐Ÿšจ ์ฃผ์˜
์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ ๋งค ์‹œ๊ฐ„๋‹น ํฌ๋ ˆ์ธ๋“ค์ด index๊ฐ€ 0์ธ ์ปจํ…Œ์ด๋„ˆ๋“ค๋ถ€ํ„ฐ ํƒ์ƒ‰์„ ํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋”๋‹ˆ ์ปจํ…Œ์ด๋„ˆ ์ด๋™์ด ์™„๋ฃŒ๊ฐ€ ๋๊ฑฐ๋‚˜ ์ž์‹ ์ด ์˜ฎ๊ธธ ์ˆ˜ ์—†๋Š” ๋ฌด๊ฒŒ์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋ถˆํ•„์š”ํ•œ ํƒ์ƒ‰์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ•˜๊ฒŒ๋˜์–ด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์™”๋‹ค.


๊ทธ๋ž˜์„œ start๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๋ฐฐ๋ฒˆ crane์ด ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ๋„๋ก ํ•˜์˜€๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๊ณ  ๋ฌธ์ œ๊ฐ€ ๋ฌด์‚ฌํžˆ ํ†ต๊ณผํ–ˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป ์ž‘์„ฑํ•œ ์ฝ”๋“œ

import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		// ํฌ๋ ˆ์ธ ๋ฌด๊ฒŒ ๋ฐ›๊ธฐ
		int N = Integer.parseInt(br.readLine());
		StringTokenizer st = new StringTokenizer(br.readLine());
		Integer[] crane = new Integer[N];
		for (int i=0; i<N; i++) {
			crane[i] = Integer.parseInt(st.nextToken());
		}
		
		// ์ปจํ…Œ์ด๋„ˆ ๋ฌด๊ฒŒ ๋ฐ›๊ธฐ
		int M = Integer.parseInt(br.readLine());
		st = new StringTokenizer(br.readLine());
		Integer[] container = new Integer[M];
		for (int i=0; i<M; i++) {
			 container[i] = Integer.parseInt(st.nextToken());
		}
		
		// ํฌ๋ ˆ์ธ๊ณผ ์ปจํ…Œ์ด๋„ˆ ๋ฌด๊ฒŒ ์ •๋ ฌ
		Arrays.sort(crane, Collections.reverseOrder());
		Arrays.sort(container, Collections.reverseOrder());
		
		if (container[0] > crane[0]) System.out.println(-1);
		else {
			int result = 0;
			int[] start = new int[N];
			Arrays.fill(start, 0);
			while (M > 0) {
				for (int i=0; i<N; i++) { // ๊ฐ๊ฐ์˜ crane์ด ์ผํ•จ
					if (M < 1) break;
					for (int j=start[i]; j<container.length; j++) {
						if (container[j]==0 || container[j] > crane[i]) {
							start[i]++;
							continue;
						}
						container[j] = 0;
						M--;
						break;
					}	
				}
				result++;
			}
			System.out.println(result);
		}
	}
}

profile
๋ธ”๋กœ๊ทธ ์ด์ „ํ–ˆ์Šต๋‹ˆ๋‹ค. -> https://seongwon.dev/

0๊ฐœ์˜ ๋Œ“๊ธ€