진기의 최고급 붕어빵

Updated:

  • d3
  • 첫번째 코드는 배열의 인덱스를 second로 사용해서 푸는 방법
  • 두번째는 배열에 각 사람이 오는 시간을 넣어서 푸는 법

  • 다시 풀어본 알고리즘
  • 첫번째 방법
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int tc = 1; tc <= T; tc++) {

			int N = sc.nextInt();
			int M = sc.nextInt();
			int K = sc.nextInt();

			int[] second = new int[11112];

			for (int i = 0; i < N; i++) {
				int index = sc.nextInt();
				second[index]++;
			}

			System.out.print("#" + tc + " ");

			int time = 0;
			if (second[time] != 0)
				System.out.print("Impossible");
			else {
				int count = 0;
				int n_count = 0;
				while (true) {
					time++;
					if (time % M == 0)
						count += K;
					if (second[time] != 0) {
						count -= second[time];
						n_count += second[time];
					}
					if (count < 0) {
						System.out.print("Impossible");
						break;
					}
					if (n_count == N) {
						System.out.print("Possible");
						break;
					}
				}
			}
			System.out.println();
		}
	}
}
  • 두번째 방법

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

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int tc = 1; tc <= T; tc++) {

			int N = sc.nextInt();
			int M = sc.nextInt();
			int K = sc.nextInt();

			int[] client = new int[N];
			for (int i = 0; i < N; i++)
				client[i] = sc.nextInt();

			int bread = 0;

			Arrays.sort(client);
      //정렬을 해주어야 한다.
			System.out.print("#" + tc + " ");
			if (client[0] == 0)
				System.out.println("Impossible");
			else {
				int second = 1;
				int idx = 0;
				boolean flag = true;

				while (second <= client[N - 1]) {
					if (second % M == 0)
						bread += K;
					while (idx < N && second == client[idx]) {
						idx++;
						bread--;
					}
					if (bread < 0) {
						flag = false;
						break;
					}
					second++;
				}

				if (flag)
					System.out.println("Possible");
				else
					System.out.println("Impossible");
			}
		}
	}
}

Leave a comment