practivceAlgorithm/백준 379

[백준][Python] 18115 카드 놓기

deque와 맨앞 요소만 책임지는 front배열을 두어 운용했습니다. import sys input = sys.stdin.readline from collections import deque N = int(input()) a = list(map(int, input().split()))[::-1] floor = [i for i in range(N, 1, -1)] front = [1] q = deque() for command in a[1:]: if command == 1: q.appendleft(front.pop()) front.append(floor.pop()) elif command == 2: q.appendleft(floor.pop()) else: q.append(floor.pop()) answer ..

[백준][Python] 16507 어두운 건 무서워

가장 자주나오는 prefix-sum 문제 import sys input = sys.stdin.readline R, C, Q = map(int, input().split()) matrix = [list(map(int, input().split())) for _ in range(R)] dp = [[0 for _ in range(C + 1)] for _ in range(R + 1)] for i in range(1, R + 1): for j in range(1, C + 1): dp[i][j] = -dp[i-1][j-1] + dp[i-1][j] + dp[i][j-1] + matrix[i-1][j-1] for i in range(Q): r1, c1, r2, c2 = map(int, input().split()) a..

[백준][Python] 2670 연속 부분 최대곱

나냐 전이냐. 보통 내가 1보다 크면 무조건 큰 값이 나온다. 근데 만약 1보다 작은 값이 계속되어 1 미만으로 떨어질 때 나보다 작아지면 굳이 그 연속 부분을 끌고갈 필요가 없다. import sys input = sys.stdin.readline N = int(input()) li = [float(input()) for _ in range(N)] for i in range(1, N): li[i] = max(li[i], li[i]*li[i-1]) print(f"{max(li):.3f}") 스터디 다른분이 소개해주신 카데인 알고리즘. import sys input = sys.stdin.readline N = int(input()) cur = 1 answer = 1 max_num = 0 for _ in r..

[백준][Python] 20164 홀수 홀릭 호석

평범한 분할정복문제입니다. 3분할할때 칸막이 설치 위치만 잘 고려해주면 됩니다. import sys input = sys.stdin.readline def count_odd(n): cnt = 0 for i in n: if int(i) & 1: cnt += 1 return cnt def divided(n, cnt): global max_v, min_v s = str(n) cnt += count_odd(s) if len(s) == 1: min_v = min(min_v, cnt) max_v = max(max_v, cnt) return elif len(s) == 2: divided(n // 10 + n % 10, cnt) else: for i in range(1, len(s) - 1): for j in rang..

[백준][Python] 19948 음유시인영재

시와 제목을 잇고 딕셔너리에 횟수를 저장, 1씩 빼주는 식. 다음꺼랑 문자 같으면 조사 x import sys input = sys.stdin.readline poem = input().rstrip() title = ''.join([a[0].upper() for a in poem.split()]) poem += title + '.' left_space = int(input()) left_push_alphabet = {idx: cnt for idx, cnt in enumerate(map(int, input().split()))} for i in range(len(poem) - 1): if poem[i] == poem[i + 1]: continue if poem[i] == ' ': if left_space:..

[백준][Python] 1374 강의실

강의실 배정문제. 여러번 풀었던 문제. 방을 몇개 가져갈꺼냐? 끝난시간이면 빼고 갈아끼워준다. import sys input = sys.stdin.readline from heapq import heappop, heappush n = int(input()) heap = [] count = 0 for _ in range(n): num, start, end = map(int, input().split()) heappush(heap, [start,end,num]) classroom = [] start, end, num = heappop(heap) heappush(classroom, end) while heap: start, end, num = heappop(heap) if classroom[0]

[백준][Python] 1080 행렬

쭉 가면서 계속 바꿔주면 된다. 아니면 누적 배열을 만들어 바꾼 횟수를 기록해주며 홀짝 판별해도 되는데 소요가 비슷하기 때문에 그냥 그리디로 계속 바꾸는게 낫다. import sys input = sys.stdin.readline N, M = map(int, input().split()) A = [list(map(int, list(input().rstrip()))) for _ in range(N)] B = [list(map(int, list(input().rstrip()))) for _ in range(N)] cnt = 0 for i in range(N): for j in range(M): if i < N - 2 and j < M - 2: if A[i][j] != B[i][j]: for x in rang..