[백준] 13458번_시험 감독

Arin·2025년 11월 27일

🔗문제 링크 바로가기

아래 첫 번째 코드로 제출했을 때 시간초과가 났다.

문제점

  1. a[i]값이 엄청 커지면 while루프를 해당 값만큼 반복한다. -> 시간초과 발생
  2. 총감독관은 각 시험장에 1명밖에 못들어가기 때문에 총감독관을 처음에 1명 배치했으면 나머지 응시자는 부감독관으로 처리해야한다.
    따라서 if(b>c)로 두 종류의 감독관을 나눌 필요 없이 부감독관만 할당하면 된다.
import sys
input = sys.stdin.readline

n = int(input()) # 시험장 개수 (1~)


input_string = input() # 문자열
a = list(map(int, input_string.split())) # 문자열 리스트 -> int로 변환(각 시험장의 응시자 수)

b, c = map(int, input().split())

result = 0

for i in range(n):
    if(a[i] > 0): # 응시자가 존재하면 
        a[i] = a[i] - b # 총감독관 한 명 먼저 배치
        result = result + 1 # 감독관 수 1 증가

    while(a[i] > 0): # 응시자가 존재하면 아래 반복
        if(b > c):
            a[i] = a[i] - b
        else:
            a[i] = a[i] - c
        result = result + 1 # 감독관 수 1 증가


print(result)

해결

  1. while루프를 몫과 나머지 연산으로 대체
  2. b와 c값 비교하지 않고 바로 c값으로 처리
import sys
input = sys.stdin.readline

n = int(input()) # 시험장 개수 (1~)


input_string = input() # 문자열
a = list(map(int, input_string.split())) # 문자열 리스트 -> int로 변환(각 시험장의 응시자 수)

b, c = map(int, input().split())

result = 0

for i in range(n):
    a[i] = a[i] - b # 총감독관 한 명 먼저 배치
    result = result + 1 # 감독관 수 1 증가
    
    if(a[i] > 0):    
        quote = a[i] // c
        result = result + quote 
        #print("몫: result = ", result)
        if((a[i] % c) != 0):
            result = result + 1
            #print("나머지 추가: result = ", result)
    

print(result)
profile
헤맨만큼 내 땅

0개의 댓글