[BaekJoon] #13300 - 방 배정 [Java][C++][Python]
[BaekJoon] #13300 - 방 배정 [Java][C++][Python]
1. 아이디어
같은 학년의 같은 성별인 학생끼리만 같은 방을 쓸 수 있으므로 학년과 성별마다 학생 수를 센다. 한 그룹의 학생이 $c$명이고 한 방에 최대 $K$명이면 그 그룹에는 $\lceil c / K \rceil$개의 방이 필요하고, 그룹끼리는 방을 섞을 수 없으므로 그룹마다 올림한 값을 모두 더한 것이 답이다. 올림은 정수 나눗셈으로 $(c + K - 1) / K$와 같고 학생이 없는 그룹($c = 0$)은 0이 나와 따로 거를 필요가 없다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[][] cnt = new int[1 + 6][2];
while (n-- > 0) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
cnt[y][s]++;
}
int ans = 0;
for (int i = 1; i <= 6; i++) {
for (int j = 0; j <= 1; j++) {
ans += (cnt[i][j] + k - 1) / k;
}
}
System.out.println(ans);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <bits/stdc++.h>
using namespace std;
int cnt[1 + 6][2];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
while (n--) {
int s, y;
cin >> s >> y;
cnt[y][s]++;
}
int ans = 0;
for (int i = 1; i <= 6; i++) {
for (int j = 0; j <= 1; j++) {
ans += (cnt[i][j] + k - 1) / k;
}
}
cout << ans;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
cnt = [[0] * 2 for _ in range(1 + 6)]
for _ in range(n):
s, y = map(int, input().split())
cnt[y][s] += 1
ans = 0
for y in range(1, 7):
for s in range(2):
ans += (cnt[y][s] + k - 1) // k
print(ans)
This post is licensed under CC BY 4.0 by the author.