• 문제 링크
20974번: Even More Odd Photos
In this example, one way to form the maximum number of five groups is as follows. Place 2 in the first group, 11 in the second group, 13 and 1 in the third group, 15 in the fourth group, and 17 and 3 in the fifth group.
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));
int n = read(), e = 0, o = 0;
while (n-- > 0) {
if (read() % 2 == 0) e++;
else o++;
}
boolean flag = true;
int cnt;
for (cnt = 0; ; flag = !flag, cnt++) {
if (flag) {
if (e > 0) e--;
else if (o > 1) o -= 2;
else break;
} else {
if (o > 0) o--;
else break;
}
}
bw.write(String.valueOf(o == 0 ? cnt : cnt - 1));
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' 카테고리의 다른 글
[백준] 11968 High Card Wins - Greedy / Java (0) | 2023.12.22 |
---|---|
[백준] 16406 Exam - Greedy / Java (0) | 2023.12.21 |
[백준] 15761 Lemonade Line - Greedy / Java (0) | 2023.12.19 |
[백준] 28323 불안정한 수열 - Greedy / Java (0) | 2023.12.18 |
[백준] 4993 Red and Black - Graph Theory / Java (0) | 2023.12.17 |
댓글