데이터사이언스 기록기📚

[백준/Python] 2852번(구현, 문자열)_NBA 농구 본문

Coding Test/백준(Python)

[백준/Python] 2852번(구현, 문자열)_NBA 농구

syunze 2023. 4. 25. 15:53

📌문제 유형

구현, 문자열(실버3)

 

📌문제

 

2852번: NBA 농구

첫째 줄에 골이 들어간 횟수 N(1<=N<=100)이 주어진다. 둘째 줄부터 N개의 줄에 득점 정보가 주어진다. 득점 정보는 득점한 팀의 번호와 득점한 시간으로 이루어져 있다. 팀 번호는 1 또는 2이다. 득

www.acmicpc.net

 

📌나의 문제풀이

- 성공한 문제풀이

  • 시작시간을 -1로 두고 시작(0으로 둘 경우, 시작시간이 00:00과 겹쳐 계산 안됨)
  • 시간 계산하는 경우 : (동점일 때, 48:00일 때 이기고 있는 경우에) 시간 - 이기고 있던 팀 시간
    • 시간 계산 위해 이전에 이기고 있는 팀 저장(team_win)
# 주의, 시작시간이 0일 수 있음!!
n = int(input())
score_1 = [0,-1] #[점수, 시작 시간]
score_2 = [0,-1] #[점수, 시작 시간]
team_win = []

ans = [0,0]

for i in range(n):
    team, time = map(str,input().split())
    team = int(team)
    hour, minute = map(int,time.split(':'))
    t = hour * 60 + minute

    # 점수 계산
    if team == 1:
        score_1[0] += 1
        # 점수가 같지 않고, 시작 시간이 없는 경우
        if score_1[1] == -1 and score_2[0] != score_1[0]:
            score_1[1] = t
    elif team == 2:
        score_2[0] += 1
        # 점수가 같지 않고, 시작 시간이 없는 경우
        if score_2[1] == -1 and score_2[0] != score_1[0]:
            score_2[1] = t

    # 이기는 팀 넣기
    if score_1[0] > score_2[0]:
        team_win.append(1)
    elif score_1[0] < score_2[0]:
        team_win.append(2)

    # 동점인 경우
    if score_2[0] == score_1[0]:
        if team_win[-1] == 1:
            ans[0] += (t-score_1[1])
        elif team_win[-1] == 2:
            ans[1] += (t-score_2[1])
        score_1[1], score_2[1] = -1, -1
        team_win = []

# 48분일 때 계산
final = 48 * 60
if team_win != []:
    if team_win[-1] == 1:
        ans[0] += (final-score_1[1])
    elif team_win[-1] == 2:
        ans[1] += (final-score_2[1])

for t_ in ans:
    h = t_//60
    h = str(h)
    m = t_%60
    m = str(m)

    if len(h) == 1:
        h = '0'+h
    if len(m) == 1:
        m ='0'+m
    print(h+':'+m)

- 실패

n = int(input())
score = [[0,'00:00'],[0,'00:00']]
ans = [[0,0],[0,0]]

def time_calculate(time1, time2):
    time1_m, time1_s = map(int,time1.split(':'))
    time2_m, time2_s = map(int,time2.split(':'))

    if time1_s - time2_s < 0:
        s = (60-time2_s) + time1_s
        m = (time1_m-1) - time2_m
    else:
        s = time1_s - time2_s
        m = time1_m - time2_m
    
    return m,s


for _ in range(n):
    team, time = list(map(str, input().split()))
    bs_1, bs_2 = score[0][0], score[1][0]   # 이전 점수 저장
    # 1:0 or 0:1인 경우
    
    if team == '1':
        score[0][0] += 1
        if (score[0][0] == 1 and score[1][0] == 0) or score[0][0] == score[1][0]:
            score[0][1] = time
    else:
        score[1][0] += 1
        if (score[1][0] == 1 and score[0][0] == 0) or score[0][0] == score[1][0]:
            score[1][1] = time

    # 동점인 경우
    if score[0][0] == score[1][0]:
        max_ = max(score[0][1], score[1][1])
        min_ = min(score[0][1], score[1][1])
        m,s = time_calculate(max_, min_)
        # min값인 곳에 넣기
        if score[0][1] > score[1][1]:
            ans[1][0] += m
            ans[1][1] += s
        else:
            ans[0][0] += m
            ans[0][1] += s
        score[0][1], score[1][1] = max_,max_

    # 2:1 or 1:2인 경우
    if score[0][0] > score[1][0]:
        if bs_1 == bs_2:
            score[0][1] = time
        else:
            before_time = score[0][1]
            m,s = time_calculate(time, before_time)
            ans[0][0] += m
            ans[0][1] += s
            score[0][1] = time
    elif score[0][0] < score[1][0]:
        if bs_1 == bs_2:
            score[1][1] = time
        else:
            before_time = score[1][1]
            m,s = time_calculate(time, before_time)
            ans[1][0] += m
            ans[1][1] += s
            score[1][1] = time

    
    # print(score)
    # print(ans)
    # print()

if score[0][0] > score[1][0]:
    m,s = time_calculate('48:00', score[0][1])
    ans[0][0] += m
    ans[0][1] += s

elif score[0][0] < score[1][0]:
    m,s = time_calculate('48:00', score[1][1])
    ans[1][0] += m
    ans[1][1] += s

for i in range(2):
    if len(str(ans[i][0])) == 1:
        ans[i][0] = '0'+str(ans[i][0])
    if len(str(ans[i][1])) == 1:
        ans[i][1] = '0'+str(ans[i][1])
    print(str(ans[i][0]) + ':' + str(ans[i][1]))

 

📌 다른사람의 문제풀이

 

[BOJ]2852. NBA 농구

문제: 동혁이는 NBA 농구 경기를 즐겨 본다. 동혁이는 골이 들어갈 때 마다 골이 들어간 시간과 팀을 적는 이상한 취미를 가지고 있다. 농구 경기는 정확히 48분동안 진행된다. 각 팀이 몇 분동안

codedrive.tistory.com

n = int(input())
timeline = []

for _ in range(n):
    timeline.append(input().split())

timeline = sorted(timeline, key = lambda x: x[1])
timeline.append([-1, '48:00'])

one, two = 0, 0
pivot = ''

one_ans, two_ans = 0,0
for team, time in timeline:
    if one == two:
        pass
    elif one > two:
        min_, sec = map(int,time.split(':'))
        min_p, sec_p = map(int, pivot.split(':'))
        one_ans += (min_*60+sec) - (min_p*60+sec_p)
    else:
        min_, sec = map(int,time.split(':'))
        min_p, sec_p = map(int,pivot.split(':'))
        two_ans += (min_*60+sec) - (min_p*60+sec_p)
    pivot = time
    if int(team) == 1:
        one += 1
    elif int(team) == 2:
        two += 1

q,r = divmod(one_ans, 60)
print(str(q).zfill(2) + ':' + str(r).zfill(2))
q,r = divmod(two_ans, 60)
print(str(q).zfill(2) + ':' + str(r).zfill(2))

 

[백준|python] 2852번 NBA 농구

이 문제에서 주의해야 할 점은 player1이 이기다가.. 다시 비기게 되는 순간에 player1이 이기는 시간이 멈추어져야 하고, player2가 다시 선방(?)하게 될 때 또 player2의 이기는 순간이 시작되어야 한다.

kau-algorithm.tistory.com

 

 

📌 리뷰 

- 시간 계산 : 초로 바꿔서 계산 → 분 = ans//60 , 초 = ans%60

- 아이디어는 맞았으나 코드로 구체화하지 못함ㅠㅠ

- 양수, 0, 음수로 구분할 수도 있음

728x90
Comments