13458_시험 감독 (Python)

0. 출처

1. 기록

  • 22/04/20 (수)

2. 풀이

(1) 아이디어, 시간복잡도, 자료구조

1. 아이디어
>> 각 시험장에 있는 사람들에서 b를 빼고 그 결과를 c로 계속나눠서 그 몫을 구한다.
>> c로 나눴을 때 나머지가 있는 경우 몫에 + 1 해주고 나머지가 없는경우 그 몫만 가지고온다.

2. 시간복잡도
>>

3. 자료구조
>>

(2) 예제 입력, 예제 출력

- 예제 입력 1 -
1   (시험장의 갯수)
1   (각 시험장에 있는 응시자 수)
1 1 (감시할 수 있는 응시자 수 / 감시할 수 있는 응시자 수)

- 예제 출력 1 -
1

- 예제 입력 2 -
3
3 4 5
2 2

- 예제 출력 2 -
7

- 예제 입력 3 (반례) -
8
5 10 30 235 1 23 24 101
10 3

- 예제 출력 3
131

(3) 코드

# 틀린 코드
import math

n = int(input())
applicants = list(map(int, input().split()))
b, c = map(int,input().split())

answers = 0
for applicant in applicants:
    applicant = applicant - b

    # 나머지가 있는 경우
    if applicant%c > 0 :
        answer = applicant // c
        answer = math.floor(answer+1)

    # 나머지가 없는 경우
    elif applicant%c == 0:
        answer = applicant // c

    answers += answer+1

print(answers)
# 정답 코드

import math

n = int(input())
applicants = list(map(int, input().split()))
b, c = map(int,input().split())

answers = 0

for applicant in applicants:
    cnt = 0
    answer = 0
    if b >= applicant:
        cnt += 1

    elif b < applicant:
        applicant = applicant - b
        cnt += 1

        # 나머지가 있는 경우
        if applicant%c > 0 :
            portion = applicant // c
            portion = math.floor(portion+1)
            cnt += portion

        # 나머지가 없는 경우
        elif applicant%c == 0:
            portion = applicant // c
            cnt += portion

    answers += cnt

print(answers)

(4) 정리

주시험감독이 감시할 수 있는 수가 참가자보다 큰 경우인지 아닌지를 처음에 판단해줘야 합니다.
그렇지 않고 그냥 빼버리면 참가자가 마이너스가 되는 경우가 생겨서 한참 헤맸습니다.

(5) 참고

참고 반례

+ Recent posts