Skip to content

Commit d9eeea6

Browse files
authored
Merge pull request #756 from AlgorithmWithGod/khj20006
[20250827] BOJ / P3 / 습격자 초라기 / 권혁준
2 parents 537a278 + 5d9d01b commit d9eeea6

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static final int INF = (int)1e9 + 7;
57+
58+
static int N, W;
59+
static int[][] arr;
60+
61+
public static void main(String[] args) throws Exception {
62+
63+
io = new IOController();
64+
65+
for(int T=io.nextInt();T-->0;) {
66+
N = io.nextInt();
67+
W = io.nextInt();
68+
arr = new int[N+1][2];
69+
for(int i=1;i<=N;i++) arr[i][0] = io.nextInt();
70+
for(int i=1;i<=N;i++) arr[i][1] = io.nextInt();
71+
72+
int ans = Integer.MAX_VALUE;
73+
arr[0][0] = arr[0][1] = INF;
74+
ans = Math.min(ans, get(2));
75+
arr[0][0] = arr[N][0];
76+
arr[N][0] = INF;
77+
ans = Math.min(ans, get(1));
78+
arr[N][0] = arr[0][0];
79+
arr[0][0] = INF;
80+
arr[0][1] = arr[N][1];
81+
arr[N][1] = INF;
82+
ans = Math.min(ans, get(0));
83+
arr[0][0] = arr[N][0];
84+
arr[N][0] = INF;
85+
for(int i=N;i>=1;i--) {
86+
arr[i][0] = arr[i-1][0];
87+
arr[i][1] = arr[i-1][1];
88+
}
89+
arr[0][0] = arr[0][1] = INF;
90+
ans = Math.min(ans, get(2));
91+
io.write(ans + "\n");
92+
}
93+
94+
io.close();
95+
96+
}
97+
98+
static int get(int x) {
99+
int[][] dp = new int[N+1][3];
100+
if(x == 1) {
101+
dp[0][0] = dp[0][2] = 1;
102+
}
103+
else if(x == 0) {
104+
dp[0][1] = dp[0][2] = 1;
105+
}
106+
107+
for(int i=1;i<=N;i++) {
108+
dp[i][0] = dp[i-1][2] + 1;
109+
if(arr[i-1][0] + arr[i][0] <= W) dp[i][0] = Math.min(dp[i][0], dp[i-1][1] + 1);
110+
dp[i][1] = dp[i-1][2] + 1;
111+
if(arr[i-1][1] + arr[i][1] <= W) dp[i][1] = Math.min(dp[i][1], dp[i-1][0] + 1);
112+
dp[i][2] = dp[i-1][2] + 2;
113+
if(arr[i-1][0] + arr[i][0] <= W) dp[i][2] = Math.min(dp[i][2], dp[i-1][1] + 2);
114+
if(arr[i-1][1] + arr[i][1] <= W) dp[i][2] = Math.min(dp[i][2], dp[i-1][0] + 2);
115+
if(arr[i-1][0] + arr[i][0] <= W && arr[i-1][1] + arr[i][1] <= W) dp[i][2] = Math.min(dp[i][2], dp[i-2][2] + 2);
116+
if(arr[i][0] + arr[i][1] <= W) dp[i][2] = Math.min(dp[i][2], dp[i-1][2] + 1);
117+
}
118+
return dp[N][x];
119+
}
120+
121+
}
122+
```

0 commit comments

Comments
 (0)