[Python] BOJ 17390 - 이건 꼭 풀어야 해!

728x90
반응형

https://www.acmicpc.net/problem/17390

 

17390번: 이건 꼭 풀어야 해!

[2, 5, 1, 2, 3]을 비내림차순으로 정렬하면 [1, 2, 2, 3, 5]이다.

www.acmicpc.net

# 1차원 누적합 문제이다.

 

1. 수열 num을 입력받은 뒤, 비내림차순으로 정렬해준다.

 

2. prefix_sum에 수들의 누적합을 구해서 append해준다.

 

3. Q만큼 질문의 답을 출력해준다.

    L R = B[L] + B[L+1] + .. + B[R-1] + B[R] 이므로 prefix_sum[R] - prefix_sum[L-1]을 출력하면 된다.

 

import sys
input = sys.stdin.readline
N, Q = map(int,input().split())
num = list(map(int,input().split()))
num.sort()
prefix_sum = [0]
for i in range(N):
    prefix_sum.append(prefix_sum[i] + num[i])

for _ in range(Q):
    L, R = map(int,input().split())
    print(prefix_sum[R] - prefix_sum[L-1])

(solved.ac 티어: 실버 3)

728x90
반응형
TAGS.

Comments