주유소

 

13305번: 주유소

표준 입력으로 다음 정보가 주어진다. 첫 번째 줄에는 도시의 개수를 나타내는 정수 N(2 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 인접한 두 도시를 연결하는 도로의 길이가 제일 왼쪽 도로부터 N-1

www.acmicpc.net

 

내 코드

from sys import stdin

N = int(stdin.readline())
kmList = list(map(int, stdin.readline().split()))
oilList = list(map(int, stdin.readline().split()))
pay = 0
curIdx = 0

while True:
    # print(curIdx)
    if curIdx >= len(oilList)-1:
        break

    # 현재 주유소에서 나보다 주유소 가격이 싼경우 전까지 다 사야함
    nextIdx = curIdx + 1
    for idx in range(curIdx +1, len(oilList)):
        if idx == len(oilList) - 1:
            nextIdx = idx
        if oilList[idx] < oilList[curIdx]:
            nextIdx = idx
            break
    # print("nextId", nextIdx)

    # 구매한 km oil 계산
    for idx in range(curIdx, nextIdx):
        pay += oilList[curIdx] * kmList[idx]

    curIdx = nextIdx
    # print("pay", pay)
    # print()
print(pay)
728x90

카드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

+ Recent posts