Skip to content

Commit 272ac53

Browse files
beberichetony9402
andauthored
[ADD] baekjoon 13422 java solution (#85)
* [ADD] baekjoon 13422 java solution * [UPD] solution description 수정 * Refactoring --------- Co-authored-by: Minsang Kim <[email protected]>
1 parent 40440fe commit 272ac53

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

solutions/baekjoon/13422/Main.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Authored by: beberiche
2+
// Co-authored by: -
3+
// Link: http://boj.kr/e160208f4a324b34b406ca253ca6194f
4+
5+
import java.util.*;
6+
import java.io.*;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
FastReader rd = new FastReader();
11+
int T = rd.nextInt();
12+
13+
StringBuffer sb = new StringBuffer();
14+
while (--T >= 0) {
15+
int N = rd.nextInt();
16+
int M = rd.nextInt();
17+
int K = rd.nextInt();
18+
19+
int a[] = new int[N + M];
20+
21+
for (int i = 1; i <= N; i++) {
22+
a[i] = a[i - 1] + rd.nextInt();
23+
}
24+
25+
if (N == M) {
26+
sb.append(a[N] < K ? 1 : 0).append("\n");
27+
continue;
28+
}
29+
30+
int idx = N + 1;
31+
for (int i = 1; i < M; i++) {
32+
a[idx] = a[idx - 1] + a[i] - a[i - 1];
33+
idx++;
34+
}
35+
36+
int ret = 0;
37+
for (int i = 0; i < N; i++) {
38+
if (a[i + M] - a[i] < K) ret++;
39+
}
40+
sb.append(ret).append("\n");
41+
}
42+
System.out.print(sb.toString());
43+
}
44+
45+
static class FastReader {
46+
BufferedReader br;
47+
StringTokenizer st;
48+
49+
public FastReader() {
50+
br = new BufferedReader(new InputStreamReader(System.in));
51+
}
52+
53+
String next() {
54+
while (st == null || !st.hasMoreElements()) {
55+
try {
56+
st = new StringTokenizer(br.readLine());
57+
} catch (IOException e) {
58+
e.printStackTrace();
59+
}
60+
}
61+
return st.nextToken();
62+
}
63+
64+
int nextInt() {
65+
return Integer.parseInt(next());
66+
}
67+
68+
long nextLong() {
69+
return Long.parseLong(next());
70+
}
71+
72+
double nextDouble() {
73+
return Double.parseDouble(next());
74+
}
75+
76+
String nextLine() {
77+
String str = "";
78+
try {
79+
str = br.readLine();
80+
} catch (IOException e) {
81+
e.printStackTrace();
82+
}
83+
return str;
84+
}
85+
}
86+
}
87+
88+
/* Solution Description
89+
90+
1. 누적합을 활용한 문제. 특정 조건이 존재하므로,
91+
여러 테스트 케이스를 반영해보지 않으면, 맞왜틀에 빠질 수 있는 문제이다.
92+
93+
2. 입력값을 기준으로 누적합을 구한후, 누적합의 차를 활용하여 연속되는 집의 갯수 `M` 의 합계를 구한다.
94+
단, 마을의 집은 처음의 집과 마지막 집이 이어지기 때문에 `M-1` 개 만큼, 입력 후반 부에서 처음의 집을 지나는 누적합을 추가적으로 구했다.
95+
96+
3. 입력의 제한사항을 `M<=N`으로 `N`과 `M` 이 서로 동일할 수 있다.
97+
`N` 과 `M` 이 동일한 경우에는 누적합의 차가 항시 동일하기 때문에, `N` 만큼의 탐색동안 중복된 카운트가 반영될 수 있다.
98+
99+
4. 위의 조건 외에는 누적합 간 `M` 범위의 차를 통해 돈을 훔치는 방법의 가짓수를 구할 수 있다.
100+
101+
*/

0 commit comments

Comments
 (0)