본문 바로가기
Problem Solving/SWEA

[ SWEA ] D3 - 5515, 1208, 4466, 1229, 3142 - python 문제풀이

by IM조이 2021. 7. 7.

5515 2016년 요일맞히기

규칙을 찾으면 쉽게 풀리는 문제

info = [31,29,31,30,31,30,31,31,30,31,30,31]
r = {0:3,1:4,2:5,3:6,4:0,5:1,6:2} # 요일에 대응되는 숫자
for tc in range(int(input())):
    m,d = map(int, input().split())
    print("#{} {}".format(tc+1, r[d%7] if m==1 else r[(sum(info[:m-1])+d)%7]))

 

1208 Flatten
접근법 : 정렬 -> 최대최소 차이비교 -> 반복 ... 만약 차이가 1과 같거나 작아지면 이제 의미 없는 블록 옮기기라 탈출

for tc in range(10):
    N = int(input())
    blocks = sorted(list(map(int, input().split())))
    while N > 0:
        blocks[-1] -= 1
        blocks[0] += 1
        blocks.sort()
        if blocks[-1]-blocks[0] <= 1:
            break
        N -= 1
    print("#{} {}".format(tc+1, blocks[-1]-blocks[0]))

 

4466 최대성적표만들기

r = []
for tc in range(int(input())):
    N,K = map(int, input().split())
    scores = sorted(list(map(int, input().split())))
    r.append("#{} {}".format(tc+1, sum(scores[N-1:N-1-K:-1])))
print("\n".join(r))

 

1229 암호문2

for tc in range(10):
    a = int(input())
    b = list(map(int, input().split()))
    c = int(input())
    d = list(input().split())
    for i in range(len(d)):
        if d[i] == 'I':
            idx = int(d[i+1])
            nums = int(d[i+2])
            for j in range(nums):
                b.insert(idx+j,int(d[i+2+(j+1)]))
        elif d[i] == 'D':
            idx = int(d[i+1])
            nums = int(d[i+2])
            for j in range(nums):
                del b[idx]
        else:
            continue
    print('#{} {}'.format(tc+1,' '.join(map(str,b[:10]))))

 

3142 영준이와 신비한 뿔의 숲
어렵게 고민하는것보다 학교다닐 때 배웠던 관계식 만들면 시간복잡도 상수로 풀이 가능

for tc in range(int(input())):
    n,m = map(int, input().split())
    print("#{} {} {}".format(tc+1,2*m-n,n-m))

만약 상수식으로 풀지 않는다면,

for tc in range(int(input())):
    n,m = map(int, input().split())
    tmp = n//2
    while tmp >= 0:
        if 2*tmp + (m-tmp) == n:
            break
        tmp -= 1
    print("#{} {} {}".format(tc+1,m-tmp,tmp))

 

댓글