백준-1758-알바생 강호(파이썬)

0

문제생각

  • 오름차순으로 정렬을 한다.
  • 해당 정렬에서 계산을 하고 이 계산이 0보다 크면 더한다음 등수를 +1 해준다.
  • 내림차순으로 정렬한 후 똑같이 진행한다.
n=int(input())
tip=[]
for _ in range(n):
    tip.append(int(input()))

tip.sort()

ans1, ans2, cnt=0, 0, 1
for i in range(n):
    temp=tip[i]-(cnt-1)
    if temp<=0:
        continue
    else:
        cnt+=1
    ans1+=temp

tip.sort(reverse=True)
cnt=1
for i in range(n):
    temp=tip[i]-(cnt-1)
    if temp<=0:
        continue
    else:
        cnt+=1
    ans2+=temp
print(max(ans1, ans2))

< 수정 >

  • 내림차순으로만 진행을 해도 값이 구해진다.
  • 원래 주려고 했던 돈이 큰 사람을 제일 빠른 등수로 설정하면 되기 때문이다.
n=int(input())
tip=[]
for _ in range(n):
    tip.append(int(input()))

ans, cnt=0, 1

tip.sort(reverse=True)
for i in range(n):
    temp=tip[i]-(cnt-1)
    if temp<=0:
        continue
    else:
        cnt+=1
    ans+=temp
print(ans)

0개의 댓글