-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj1006.cpp
More file actions
89 lines (77 loc) · 1.56 KB
/
boj1006.cpp
File metadata and controls
89 lines (77 loc) · 1.56 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<stdio.h>
#include<vector>
#define INF 999999999
#define next (x+1) % n
#define up (y-1)
using namespace std;
int n, m, k;
vector<int>one[2];
vector<int>vis[2];
vector<int>dp[2];
int min(int x, int y){ return x < y ? x : y; }
void reset()
{
for(int i = 0; i < 2; i++){
one[i].clear();
vis[i].clear();
dp[i].clear();
one[i].resize(n+1,0);
vis[i].resize(n+1,0);
dp[i].resize(n+1,INF);
}
}
int f(int x, int y)
{
if(x >= n){
if(y == 0) return f(0,1);
else return 0;
}
if(dp[y][x] != INF) return dp[y][x];
int val = 1;
if(y == 0){
vis[y][x] += 1;
dp[y][x] = min(val+f(x+1,y),dp[y][x]);
vis[y][x] -= 1;
if(one[y][x] + one[y][next] <= m && vis[y][next] < 2){
if(vis[y][next] == 1) val--;
vis[y][x] += 2;
vis[y][next] += 2;
dp[y][x] = min(val+f(x+2,y),dp[y][x]);
vis[y][x] -= 2;
vis[y][next] -= 2;
}
return dp[y][x];
}
else{
vis[y][x] += 1;
dp[y][x] = min(val+f(x+1,y),dp[y][x]);
vis[y][x] -= 1;
if(one[y][x] + one[y][next] <= m && vis[y][next] < 2){
if(vis[y][next] == 1) val--;
vis[y][x] += 2;
vis[y][next] += 2;
dp[y][x] = min(val+f(x+2,y),dp[y][x]);
vis[y][x] = vis[y][next] = 0;
}
if(one[y][x] + one[up][x] <= m && vis[up][x] < 2){
vis[y][x] += 2;
vis[up][x] += 2;
dp[y][x] = min(f(x+1,y),dp[y][x]);
vis[y][x] -= 2;
vis[up][x] -= 2;
}
return dp[y][x];
}
}
int main()
{
scanf("%d", &k);
while(k--){
scanf("%d %d", &n, &m);
reset();
for(int i = 0; i < 2; i++)
for(int j = 0; j < n; j++)
scanf("%d", &one[i][j]);
printf(">>>>>%d<<<<<\n", f(0,0));
}
}