Home ๐Ÿฃ ๋ฐฑ์ค€ Q11659 - ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 4 (java)
Post
Cancel

๐Ÿฃ ๋ฐฑ์ค€ Q11659 - ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 4 (java)

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-03-01 แ„‹แ…ฉแ„’แ…ฎ 10 11 10

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-03-01 แ„‹แ…ฉแ„’แ…ฎ 10 11 24

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
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Q11659_๊ตฌ๊ฐ„ํ•ฉ๊ตฌํ•˜๊ธฐ4 {
	public static void main1(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");

		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());

		st = new StringTokenizer(br.readLine(), " ");
		int[] arr = new int[N + 1];

		// {5, 4, 3, 2, 1}
		for (int i = 1; i <= N; i++) {
			arr[i] += arr[i - 1] + Integer.parseInt(st.nextToken()); // ๋ˆ„์ ํ•ฉ์„ ๋ฏธ๋ฆฌ ๊ตฌํ•ด ๋”ํ•ด์คŒ
		}
		// i=1, arr[1](5)  = arr[1](x) + arr[0](x) + 5
		// i=2, arr[2](9)  = arr[2](x) + arr[1](5) + 4
		// i=3, arr[3](12) = arr[3](x) + arr[2](9) + 3
		// i=4, arr[4](14) = arr[4](x) + arr[3](12) + 2
		// i=5, arr[5](15) = arr[5](x) + arr[4](14) + 1
		// arr = {5, 9, 12, 14, 15}

		for (int i = 0; i < M; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			int start = Integer.parseInt(st.nextToken());
			int end = Integer.parseInt(st.nextToken());
			System.out.println(arr[end] - arr[start - 1]);
			// 1~3 ๋ฒˆ์งธ ์ˆซ์ž์˜ ํ•ฉ์„ ๊ตฌํ•œ๋‹ค๋ฉด
			// arr[3](12) - arr[0](x) = 12
			// 2~4 ๋ฒˆ์งธ ์ˆซ์ž์˜ ํ•ฉ์„ ๊ตฌํ•œ๋‹ค๋ฉด
			// arr[4](14) - arr[1](5) = 9
			// 5~5 ๋ฒˆ์งธ ์ˆซ์ž์˜ ํ•ฉ์„ ๊ตฌํ•œ๋‹ค๋ฉด
			// arr[5](15) - arr[4](14) = 1
		}
	}
}

/*
----- ์˜ˆ์ œ ์ž…๋ ฅ 1 -----
5 3
5 4 3 2 1
1 3
2 4
5 5
----- ์˜ˆ์ œ ์ถœ๋ ฅ 1 -----
12
9
1
 */

This post is licensed under CC BY 4.0 by the author.

โ—ERROR - ec2์— codedeploy agent ์„ค์น˜์ค‘ ์—๋Ÿฌ

๐Ÿฃ ๋ฐฑ์ค€ Q2018 - ์ˆ˜๋“ค์˜ ํ•ฉ5 (java)