본문 바로가기

1102 with 파이썬 (-) 본문

Algorithms/DP (Dynamic Programming)

1102 with 파이썬 (-)

Louisus 2020. 8. 12. 17:55
728x90
# 발전소 개수 1 <= N <= 16
# 0 <= P <+ N
# # 고장나지 않은 발전소만 가능 재활용 가능 조건 추가안해서 오류

n = int(input())
lst = [list(map(int, input().split())) for _ in range(n)]
check = list(input())
p = int(input())
dp = [50] * n
cnt_y = 0

# 열
for i, ck in enumerate(check):
    if ck == 'Y':
        cnt_y += 1
        continue

    min_val = 50
    # 행
    for j in range(n):
        if j == i:
            continue
        min_val = min(min_val, lst[j][i])
    dp[i] = min_val

need_fix = p-cnt_y
print(sum(sorted(dp)[:need_fix]))
# 발전소 개수 1 <= N <= 16
# 0 <= P <+ N
# 불가능한 경우를 제외함

n = int(input())
lst = [list(map(int, input().split())) for _ in range(n)]
check = list(input())
p = int(input())
dp = [[] for _ in range(n)]
cnt_y = 0

# 열
# 고장나지 않은 발전소만 가능
for i, ck in enumerate(check):
    if ck == 'Y':
        cnt_y += 1
        for j in range(n):
            if j == i:
                continue
            dp[j].append(lst[i][j])

new_dp = [50]*n 
for i, d in enumerate(dp):
    if check[i] == 'Y':
        continue
    else:
        new_dp[i] = min(d)

need_fix = p-cnt_y
print(sum(sorted(new_dp)[:need_fix]))
# 발전소 개수 1 <= N <= 16
# 0 <= P <+ N

n = int(input())
lst = [list(map(int, input().split())) for _ in range(n)]
check = list(input())
p = int(input())
dp = [[] for _ in range(n)]
cnt_y = 0

# 열
# 고장나지 않은 발전소만 가능
for i, ck in enumerate(check):
    if ck == 'Y':
        cnt_y += 1
        for j in range(n):
            if j == i:
                continue
            dp[j].append(lst[i][j])

new_dp = [50]*n
possbile_fix = 0
for i, d in enumerate(dp):
    # 고장난 부분이면서 수리 가능한 경우
    if check[i] == 'N' and d:
        possbile_fix += 1
        new_dp[i] = min(d)

# 현재 고장나지 않은 발전소가 P개보다 큰 경우 추가 수리 필요없음           
if p <= cnt_y:
    print(0)
# 고쳐야되는 경우
else:
    # 필요 수리 개수
    need_fix = p-cnt_y
    # 불가능
    if need_fix > possbile_fix:
        print(-1)
    # 가능
    else:
        print(sum(sorted(new_dp)[:need_fix]))
# 발전소 개수 1 <= N <= 16
# 0 <= P <+ N

n = int(input())
lst = [list(map(int, input().split())) for _ in range(n)]
check = list(input())
p = int(input())
dp = [[] for _ in range(n)]
cnt_y = 0

for i, ck in enumerate(check):
    if ck == 'Y':
        for j in range(n):
            if i != j and check[j] == 'N':
                dp[j].append(lst[i][j])

new_dp = []
for d in dp:
    if d:
        new_dp.append(min(d))

if p <= check.count('Y'):
    print(0)
else:
    need_fix = p - check.count('Y')
    if need_fix <= len(new_dp):
        print(sum(sorted(new_dp)[:need_fix]))
    else:
        print(-1)

'Algorithms > DP (Dynamic Programming)' 카테고리의 다른 글

4883 with 파이썬(-)  (0) 2020.08.13
2629 with 파이썬  (0) 2020.08.12
거스름돈 with 파이썬  (0) 2020.08.08
정수 삼각형 with 파이썬  (0) 2020.08.07
2*n 타일링 with 파이썬  (0) 2020.08.06
Comments