본문 바로가기
Algorithm(CodeTree, Python)/완전탐색1

[코드트리 구간 단위로 완전탐색] 밭의 높이를 고르게하기 python

by kurooru 2022. 12. 10.
# n, h, t 입력
n, h, t = map(int, input().split())
# field_list 입력
field_list = list(map(int, input().split()))

# 함수들
# calc(s, e)
def calc(s, e):

    # curr_cost
    curr_cost = 0

    # 해당 범위 내에서
    for i in range(s, e):
        # curr_cost에 차이 추가
        curr_cost += abs(field_list[i] - h)
    
    # 반환
    return curr_cost

# 설계
# lowest_cost
import sys
lowest_cost = sys.maxsize

# 완전 탐색 시작
for i in range(n-t+1):
    # lowest_cost 업데이트
    lowest_cost = min(lowest_cost, calc(i, i+t))

# 출력
print(lowest_cost)