최대 1 분 소요

문제링크 : https://www.acmicpc.net/problem/5430

문제 아이디어는 ‘R’ 또는 ‘D’라는 메서드가 주어지고, 주어진 문자열을 이 메서드를 통해 조작한 결과를 출력

처음에는 R이 나올 때마다 deck.reverse()를 실행했으나, 시간초과에 걸림

다음으로는 연속되는 R의 갯수를 카운터하여, 연속된 R의 갯수가 짝수이면 deck.popleft()를 실행하고

홀수이면 deck.reverse()를 한 후 deck.popleft()를 실행했음

하지만 이 경우도 결과는 33%에서 실패판정

결국, deck.reverse()을 R이 나올때마다 실행하는 것이 아니라 최종 결과 때 한 번만 실행해야 함

R의 누적 갯수가 홀수이면 deck.pop()를, 짝수이면 deck.popleft()를 실행함

결국, reverse를 최소한으로 사용해야 하는 문제

import sys
from collections import deque

def RD(method):
    global cnt
    if method == 'D':
        if cnt % 2:
            if len(deck) > 0:
                deck.pop()
        else:
            if len(deck) > 0:
                deck.popleft()
    else:
        cnt += 1
    return 1

N = int(sys.stdin.readline().rstrip())
for _ in range(N):
    answer = 0
    functions = list(sys.stdin.readline().rstrip())
    list_count = int(sys.stdin.readline().rstrip())
    string_A = sys.stdin.readline().rstrip()[1:-1]


    if len(string_A) > 0:
        list_A = list(map(int, string_A.split(',')))
    else:
        list_A = []

    deck = deque(list_A)
    cnt = 0
    if len(deck) < functions.count('D'):
        print('error')
    else:
        for function in functions:
            answer = RD(function)
        answer = ''

        if cnt % 2:
            deck.reverse()
            answer += '[' + ','.join(map(str, list(deck))) + ']'
            print(answer)
        else:
            answer += '[' + ','.join(map(str, list(deck))) + ']'
            print(answer)
            # 시간 문제 마지막으로 해야 함

카테고리:

업데이트:

댓글남기기