카드2

 

2164번: 카드2

N장의 카드가 있다. 각각의 카드는 차례로 1부터 N까지의 번호가 붙어 있으며, 1번 카드가 제일 위에, N번 카드가 제일 아래인 상태로 순서대로 카드가 놓여 있다. 이제 다음과 같은 동작을 카드가

www.acmicpc.net

 

내 코드

from sys import stdin
from collections import deque

N = int(stdin.readline())
if N == 1:
    print(1)
else:
    deq = deque([i for i in range(1, N+1)])
    while True:
        deq.popleft()
        if len(deq) == 1:
            break
        deq.append(deq.popleft())
    print(deq.pop())
728x90

어두운 굴다리

 

17266번: 어두운 굴다리

인하대학교 후문 뒤쪽에는 어두운 굴다리가 있다. 겁쟁이 상빈이는 길이 조금이라도 어둡다면 가지 않는다. 따라서 굴다리로 가면 최단거리로 집까지 갈수 있지만, 굴다리는 어둡기 때문에 빙

www.acmicpc.net

 

내 코드

# N 굴다리의 길이
# M 가로등의 수
# x 가로등 설치 위치
import math
from sys import stdin
N = int(stdin.readline())
lamp = [0 for i in range(N+1)]

M = int(stdin.readline())
xList = list(map(int, stdin.readline().split()))

for x in xList:
    lamp[x] = 1
# print(lamp)

maxLen = 0
if M == 1:
    maxLen = max(xList[0], N-xList[0])
else:
    for idx in range(len(xList)):
        leftIdx = -1
        # 왼쪽 가로등 길이 계산
        if idx == 0:
            # idx == 0 이면 xList[idx] 위치 전에는 가로등이 없음
            # 오른쪽 위치 - 왼쪽 위치
            leftIdx = 0
            maxLen = max(maxLen, xList[idx] - leftIdx)
        else:
            # idx != 0 이면 xList[idx-1] 존재
            leftIdx = xList[idx-1]
            # (오른쪽 - 왼쪽) / 2
            maxLen = max(maxLen, math.ceil((xList[idx] - leftIdx)/2))

        # 오른쪽 가로등 길이 계산
        if idx + 1 < len(xList):
            # 뒤로 가로등 있는 경우
            rightIdx = xList[idx+1]
            maxLen = max(maxLen, math.ceil((rightIdx - xList[idx])/2))
        else:
            # 뒤로 가로등 없는 경우
            rightIdx = len(lamp) -1
            maxLen = max(maxLen, rightIdx - xList[idx])

print(maxLen)
728x90

크로스 컨트리

 

9017번: 크로스 컨트리

입력 데이터는 표준입력을 사용한다. 입력은 T 개의 테스트 케이스로 주어진다. 입력 파일의 첫 번째 줄에 테스트 케이스의 수를 나타내는 정수 T 가 주어진다. 두 번째 줄부터는 두 줄에 하나의

www.acmicpc.net

 

내 코드

from sys import stdin

# 한 팀은 여섯명의 선수로 구성됨
# 팀 점수는 상위 네명의 주자의 점수 합
# 점수는 자격을 갖춘 팀의 주자들에게만 주어짐 -> 결승점을 통과한 순서대로 점수를 받는다.
# 점수 가장 낮은 팀이 우승
# 동점의 경우 다섯 번째 주자가 가장 빨리 들어온 팀이 우승하게 된다.

T = int(stdin.readline())

for _t in range(T):
    # print("test case# ", _t+1)
    N = int(stdin.readline())
    team_num = list(map(int, stdin.readline().split()))
    team_dic = {}

    team_set = set(team_num)
    # 팀원수 6명이 안되는 팀은 걸러버림
    not_count_team = []
    for _ in range(len(team_set)):
        target_team = team_set.pop()
        if team_num.count(target_team) != 6:
            not_count_team.append(target_team)

    # 점수 산정
    score = 1
    for i in range(len(team_num)):
        if team_num[i] not in not_count_team:
            if team_num[i] not in team_dic:
                team_dic[team_num[i]] = [score]
            else:
                team_dic[team_num[i]].append(score)
            score += 1
    # print(team_dic)
    scores = []
    # 팀들의 점수 합산
    keys = team_dic.keys()
    for key in keys:
        _sum = sum(team_dic[key][0:4])
        scores.append(_sum)
        team_dic[key].append(_sum)

    min_score = min(scores)
    if scores.count(min_score) > 1:
        # 동점자 있음
        same_score_key = []
        for key in keys:
            if team_dic[key][-1] == min_score:
                same_score_key.append(key)
        # 동점자들 점수 다시 비교
        scores = []
        for key in same_score_key:
            scores.append(team_dic[key][-1] + team_dic[key][4])
        min_score = min(scores)
        index = scores.index(min_score)
        print(same_score_key[index])
    else:
        for key in keys:
            if team_dic[key][-1] == min_score:
                print(key)
                break
728x90

스위치 켜고 끄기

 

1244번: 스위치 켜고 끄기

첫째 줄에는 스위치 개수가 주어진다. 스위치 개수는 100 이하인 양의 정수이다. 둘째 줄에는 각 스위치의 상태가 주어진다. 켜져 있으면 1, 꺼져있으면 0이라고 표시하고 사이에 빈칸이 하나씩

www.acmicpc.net

 

내 코드

import math
from sys import stdin

# N개의 스위치 상태가 주어짐
# 남자 - 스위치 번호가 자기가 받은 수의 배수면 그 스위치의 상태 바꿈
# ex 남학생 3 -> 3번 6번 스위치의 상태를 바꿈
# 여자 - 자기가 받은 수와 같은 번호가 붙은 스위치를 중심으로 좌우가 대칭이면서 가장 많은 스위치를 포함하는 구간을 찾아
# 구간에 속하는 스위치의 상태를 모두 바꾼다. (구간에 속한 스위치 개수는 항상 홀수)

N = int(stdin.readline())
switch = list(map(int, stdin.readline().split()))

T = int(stdin.readline())

for _ in range(T):
    sex, number = map(int, stdin.readline().split())
    if sex == 1:
        # 본인이 받은 스위치의 번호의 배수 상태 변경
        # print("남자")
        multi = 1
        while True:
            index = multi * number
            if index-1 < len(switch):
                switch[index-1] = 1 if switch[index-1] == 0 else 0
                multi += 1
            else:
                break
        # print(switch)
    else:
        # print("여자")
        # 본인이 받은 수 기준 대칭 가장 많은 스위치 포함 구간 상태 변셩
        middle = number - 1
        left = middle - 1
        right = middle + 1

        while True:
            # out of range check
            if left < 0 or right >= len(switch):
                left += 1
                right -= 1
                break
            # 대칭 체크
            if switch[left] == switch[right]:
                left -= 1
                right += 1
            else:
                left += 1
                right -= 1
                break
        if left != middle and right != middle:
            for i in range(left, right+1):
                switch[i] = 1 if switch[i] == 0 else 0
        else:
            switch[middle] = 1 if switch[middle] == 0 else 0
        # print(switch)

for i in range(math.ceil(len(switch)/20)):
    print(*switch[i*20: (i+1)*20])
728x90

+ Recent posts