-
[백준] 1059. 좋은구간알고리즘 2023. 4. 7. 15:37728x90
1. 문제
https://www.acmicpc.net/problem/1059
2. 접근
1. 문제 이해가 어려운데, 좋은 구간은 ' n을 포함 & 구간안의 값들이 S에 포함 안되는 모든 경우의 수 ' 이렇게 이해해도 될거같다
2. 구간을 구하는 방법은 n이 포한되는 S의 (리스트 값+1), (다음 리스트 값)을 구하면 된다
3. 모든 경우의 수는 조합을 통해 구했다.
4. 모든 경우의 수에서 " 구간의 시작 값 > n의 값 " & " 구간의 끝 값 < n의 값 " 이면 경우의 수를 빼주면 된다
3. 풀이
import sys input = sys.stdin.readline from itertools import combinations as cb length = int(input()) s = list(map(int,input().split())) n = int(input()) res = [] s.sort() if n in s: print(0) else: start = 0 end = 0 for i in s: if i < n: start = i + 1 elif i > n: end = i break for i in range(start,end): if i > 0: res.append(i) tot = len(list(cb(res,2))) for data in list(cb(res,2)): if data[0] > n or data[1] < n: tot -= 1 print(tot)
'알고리즘' 카테고리의 다른 글
[백준] 1431 - 시리얼 번호 (0) 2023.04.07 [백준] 1302 - 베스트 셀러 (0) 2023.04.07 [프로그래머스] 실패율 (0) 2023.04.06 [프로그래머스] [3차] n진수 게임 (0) 2023.03.28 [프로그래머스] LV2. 1차 캐시 (0) 2023.03.27