-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2195C.cpp
More file actions
56 lines (39 loc) · 1.09 KB
/
2195C.cpp
File metadata and controls
56 lines (39 loc) · 1.09 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
#include <bits/stdc++.h>
using namespace std;
#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);
bool adjacent(int x, int y) {
return x != y && x != 7 - y;
}
int main() {
fast_io;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
const int INF = 1e9;
vector<vector<int>> dp(n + 1, vector<int>(7, INF));
for (int x = 1; x <= 6; x++)
dp[1][x] = (a[1] == x ? 0 : 1);
// DP
for (int i = 2; i <= n; i++) {
for (int x = 1; x <= 6; x++) {
int cost = (a[i] == x ? 0 : 1);
for (int y = 1; y <= 6; y++) {
if (adjacent(x, y)) {
dp[i][x] = min(dp[i][x],
dp[i - 1][y] + cost);
}
}
}
}
int ans = INF;
for (int x = 1; x <= 6; x++)
ans = min(ans, dp[n][x]);
cout << ans << "\n";
}
return 0;
}