반응형
문제
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.util.Scanner;
public class Main {
// USACO Even More Odd Photos JAVA
// 백준 20974
public static void main(String[] args) {
/**
* 홀수, 짝수가 각각 몇개 있는지 세어서 그룹을 만들자.
* (홀수가 2개 있으면 짝수를 만들 수 있다.)
*/
Scanner sc = new Scanner(System.in);
int odd = 0; //홀수 갯수
int even = 0; //짝수 갯수
int answer = 0; //그룹 갯수
//홀수 짝수 갯수를 세자
int n = sc.nextInt();
for(int i = 0; i < n; i++){
int temp = sc.nextInt();
if(temp % 2 == 0){
++even;
}else{
++odd;
}
}
while(true){
//짝수 그룹 증가시키기
if(even > 0) --even;
else if( odd >= 2) odd -= 2;
else break;
++answer;
//홀수 그룹 증가시키기
if (odd > 0) --odd;
else break;
++answer;
}
//모든 소들은 그룹에 속해야하는데
//홀수가 하나 남으면 어쩌지 ?
//짝수 그룹이 생겨야 할 차례인데 홀수가 혼자 남으면 그 소는 들어갈 그룹이 없다.
//기존 홀수그룹이나 짝수 그룹에 넣어버리면 그룹이 짝->홀 / 홀->짝 으로 변해버리니
//홀수그룹 + 홀수그룹 하여 짝수그룹으로 만들고 홀수소를 집어넣어줘야 해결된다.
//결론적으로, 그룹 하나를 줄이고 혼자남는 홀수 소 를 처리할 수 있다.
//(짝수는 남아도 첫번째 그룹에 집어넣는다고 가정하면 문제 없음)
if(odd > 0) --answer;
System.out.println(answer);
}
}//class
반응형
'개발 > 알고리즘' 카테고리의 다른 글
[USACO] Just Stalling JAVA (0) | 2021.02.23 |
---|---|
[USACO] Mad Scientist JAVA (0) | 2021.02.23 |
[USACO] Uddered but not Herd JAVA (0) | 2021.02.23 |
[USACO] Swapity Swap JAVA (0) | 2021.02.23 |
[USACO] Triangles JAVA (0) | 2021.02.22 |
최근댓글