chae._.chae

[Algorithm] 백준 #1205 등수 구하기 본문

파이썬 알고리즘/BOJ

[Algorithm] 백준 #1205 등수 구하기

walbe0528 2024. 7. 7. 11:04
728x90
반응형

https://www.acmicpc.net/problem/1205

 

import sys
from collections import defaultdict
input = sys.stdin.readline

N, new_score, P = map(int, input().split())
scores = list(map(int, input().split()))

# 등수 계산
def get_rank(scores, new_score):
    dict = defaultdict(int)
    for num in scores:
        dict[num] += 1
    answer = 0
    for key, value in dict.items():
        if key == new_score:
            answer += 1
            print(answer)
        else:   # 정답이 아니야
            answer += value

scores.sort(reverse=True)
if len(scores) == P:  # 꽉 찬 경우
    if scores[-1] >= new_score:  # 점수가 더 낮으면
        print(-1)
    else:
        scores.pop()  # 오른쪽 원소 제거
        scores.append(new_score)
        scores.sort(reverse=True)
        get_rank(scores, new_score)
else:
    scores.append(new_score)
    scores.sort(reverse=True)
    get_rank(scores, new_score)

 

등수 구할때, answer을 이용해서 순차적으로 나아가며 모든 점수의 등수를 각각 구하는 절차로 진행

 

# 다른 풀이
import sys
input = sys.stdin.readline

N, new, P = map(int, input().split())
if N == 0:
    print(1)
else:
    score = list(map(int, input().split()))
    if N == P and score[-1] >= new:  # 점수가 더 낮으면
        print(-1)
    else:
        for i in range(N):  # 점수리스트에 넣어서 값을 비교하면서
            if new >= score[i]:
                print(i+1)
                break
        else:  # 새로운 점수보다 작거나 같은 점수가 없다면 N+1
            print(N+1)

 

새로 입력 받은 점수를 score에 append하고, 정렬하지 않고 값을 비교하며 진행함.

728x90