Skip to content

Commit c55cfae

Browse files
authored
Merge pull request #1250 from AlgorithmWithGod/0224LJH
[20251028] BOJ / G5 / 가장 긴 짝수 연속한 수열(large)
2 parents c6fee3c + 8476c9b commit c55cfae

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.StringTokenizer;
6+
7+
public class Main {
8+
9+
10+
static int len,totalErase,curErase,start,end,ans,curLen;
11+
static int[] arr;
12+
13+
public static void main(String[] args) throws IOException {
14+
init();
15+
process();
16+
print();
17+
}
18+
19+
private static void init() throws IOException{
20+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));;
21+
StringTokenizer st = new StringTokenizer(br.readLine());
22+
len = Integer.parseInt(st.nextToken());
23+
totalErase = Integer.parseInt(st.nextToken());
24+
arr = new int[len];
25+
st = new StringTokenizer(br.readLine());
26+
for (int i = 0; i < len; i++) {
27+
arr[i] = Integer.parseInt(st.nextToken());
28+
}
29+
30+
start = 0;
31+
end = 0;
32+
ans = 0;
33+
curErase = 0;
34+
curLen = 0;
35+
36+
}
37+
38+
private static void process() {
39+
if (arr[0] %2 == 0) {
40+
curLen++;
41+
ans++;
42+
} else {
43+
curErase++;
44+
}
45+
46+
while( end < len) {
47+
if (curErase < totalErase) {
48+
end++;
49+
if (end == len)break;
50+
if (arr[end] %2 == 0) curLen++;
51+
else curErase++;
52+
53+
} else if (curErase == totalErase && end + 1 < len && arr[end+1]%2 ==0) {
54+
end++;
55+
curLen++;
56+
} else {
57+
if (arr[start] %2 == 0) {
58+
curLen--;
59+
} else {
60+
curErase--;
61+
}
62+
start++;
63+
}
64+
65+
66+
67+
ans = Math.max(ans, curLen);
68+
}
69+
}
70+
71+
72+
73+
private static void print() {
74+
System.out.println(ans);
75+
}
76+
}
77+
78+
79+
```

0 commit comments

Comments
 (0)