practivceAlgorithm/codeforce 20

[codeforce][Python] #690 E1. Close Tuples (easy version)

map으로 나보다 2큰수의 위치를 저장해둔 후 거기까지 사이의 갯수에서 나포함 3개를 뽑으면 된다. import sys input = sys.stdin.readline from bisect import bisect_left for test in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr.sort() done = {} answer = 0 for i in range(n - 2): if arr[i] not in done: right = bisect_left(arr, arr[i] + 3) done[arr[i]] = right length = done[arr[i]] - i if length > 2: length -..

[codeforce][Python] #690 C. Unique Number

숫자의 합이 되는 최소 길이의 숫자조합을 구합니다. 최소길이이므로 큰수부터 빼주며 숫자를 만들면 됩니다. 1~45까지의 모든 수는 표현이 가능하다는 것만 이해하면 됩니다. import sys input = sys.stdin.readline for test in range(int(input())): x = int(input()) answer, flag = '', 0 for num in range(9, 0, -1): if x >= num: x -= num answer += str(num) if not x: print(answer[::-1]) break else: print(-1)

[codeforce][Python] #690 B. Last Year's Substring

2020을 한번만 제외하고 만들 수 있는지 여부를 확인하는 문제입니다. 중간에 한번밖에 못띄우니 앞, 뒤에 2020의 모든 숫자가 존재해야합니다. import sys input = sys.stdin.readline for test in range(int(input())): n = int(input()) s = input().rstrip() answer = '2020' if s[0] + s[-3:] == answer or s[:2] + s[-2:] == answer or s[:3] + s[-1] == answer or s[:4] == answer or s[-4:] == answer: print("YES") else: print("NO")

[Codeforce][Python] #702 F. Equalize the Array

Counter map을 만든 후 set에 조사할 count들을 모은 후 순회하며 그 cnt보다 크면 cnt에 맞춰주고 작으면 0에 맞춰주는 식으로 풀었습니다. import sys input = sys.stdin.readline from collections import defaultdict for test in range(int(input())): n = int(input()) arr = list(map(int, input().split())) counter = defaultdict(int) for num in arr: counter[num] += 1 candidate = set() for target in counter.values(): candidate.add(target) answer = float..

[Codeforce][Python] #702 D. Permutation Transformation

분할시키면서 실제 subtree도 분할시켰습니다. import sys input = sys.stdin.readline def make_tree(depth, a): if not a: return a max_num = 0 for idx, num in enumerate(a): if max_num < num: max_idx = idx max_num = num answer[dic[max_num]] = depth make_tree(depth+1, a[:max_idx]) make_tree(depth+1, a[max_idx+1:]) for test in range(int(input())): n = int(input()) arr = list(map(int, input().split())) dic = {num: idx f..

[Codeforce][Python] #702 E. Accidental Victory

heap으로 값이 작은것부터 정렬 시킨 뒤 누적합과 내 값을 비교해가며 왕이될 수 있는 값을 구해갔습니다. import sys input = sys.stdin.readline from heapq import heappop, heappush for test in range(int(input())): n = int(input()) arr = list(map(int, input().split())) heap = [] for idx, num in enumerate(arr): heappush(heap, (num, idx)) total, answer = 0, [] while heap: num, idx = heappop(heap) if total >= num: answer.append(idx + 1) else: an..

[Codeforce][Python] #702 B. Balanced Remainders

카운팅 해준 후 넘치는거 모자란곳에 계속 옮겨줬습니다. import sys input = sys.stdin.readline def check(d): pivot = d[0] for i in range(1, 3): if d[i] != pivot: return True return False def move(over, d): global answer for i in range(3): if not over and d[i] > n // 3: pivot = d[i] - (n // 3) over.append((i, pivot)) d[i] -= pivot elif over and d[i] < n // 3: idx, num = over.pop() if idx < i: dist = num * (i - idx) else: ..