• 문제 링크
27951번: 옷걸이
경기과학고등학교에는 아주 무시무시한 부서 하나가 있다. 그 부서의 이름은 바로 '자치부'이다. '자치부'가 하는 주된 역할 중 하나는 아침에 지정된 기숙사에 들어가서 방의 정리 정돈 상태를
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int n = read();
int[] arr = new int[n], cnt = new int[4];
for (int i = 0; i < n; i++) cnt[arr[i] = read()]++;
int u = read() - cnt[1], d = read() - cnt[2];
for (int i = 0; i < n; i++) {
if (arr[i] == 1) sb.append("U");
else if (arr[i] == 2) sb.append("D");
else {
if (u > 0) {
u--;
sb.append("U");
} else if (d > 0) {
d--;
sb.append("D");
}
}
}
bw.write(u == 0 && d == 0 ? "YES\n" + sb : "NO");
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 1152 단어의 개수 - Implementation / Java (0) | 2024.03.17 |
---|---|
[백준] 15008 Falling Apart - Greedy / Java (0) | 2024.03.16 |
[백준] 15600 Boss Battle - Greedy / Java (0) | 2024.03.14 |
[백준] 30236 증가 수열 - Greedy / Java (0) | 2024.03.13 |
[백준] 28110 마지막 문제 - Greedy / Java (0) | 2024.03.12 |
댓글