1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int N = Integer.parseInt(input[0]);
int M = Integer.parseInt(input[1]);
String[] num = br.readLine().split(" ");
int[] nums = new int[N];
for (int i =0; i < N; i++)
nums[i] = Integer.parseInt(num[i]);
int[] dp = new int[N+1];
dp[0] = 0;
for (int i = 1; i < dp.length; i++)
dp[i] = dp[i - 1] + nums[i - 1];
for (int j = 0; j < M; j++)
{
String[] line = br.readLine().split(" ");
int a = Integer.parseInt(line[0]);
int b = Integer.parseInt(line[1]);
System.out.println(dp[b] - dp[a - 1]);
}
}
}
|
cs |
입력되는 값이 워낙 많아서 그런지 알고리즘 자체는 맞았는데, 계속 시간 초과가 났었다.
입출력의 시간을 줄여주는 방법을 사용해서 출ㄹ겨해 주었다.
'다이나믹 프로그래밍 한접시' 카테고리의 다른 글
[백준] 2748번: 피보나치 수 2 [C#] (0) | 2024.04.26 |
---|---|
[백준] 1932번: 정수 삼각형 [C#] (1) | 2024.04.25 |
[백준] 1149번: RGB거리 [C#] (0) | 2024.04.17 |
[백준] 9095번: 1, 2, 3 더하기 [C#] (0) | 2024.04.16 |
[백준] 1463번: 1로 만들기 [C#] (0) | 2024.04.15 |