433 字
2 分钟
Dice-Roll-Sequence

题面#

Consider the following cube DD where numbers xx and 7x7-x lie on opposite sides:

A sequence bb of integers from 11 to 66 is called a dice roll sequence if it satisfies the following condition:

  • All pairs of adjacent elements lie on adjacent ^{\text{∗}} sides of the cube.

For example, [1,4,2][1,4,2] is a dice roll sequence, while [3,4,6,3][3,4,6,3] is not because 33 and 44 are not on adjacent sides of the dice. Additionally, [2,2,4][2,2,4] is not a dice roll sequence because 22 and 22 are on the same (not adjacent) side of the dice.

Given a sequence aa of nn integers from 11 to 66, you can perform the following operation any number of times (possibly zero).

  • Select an index 1in1 \le i \le n and an integer 1x61 \le x \le 6. Then, change the value of aia_i to xx.

Please determine the minimum number of operations required to make aa a dice roll sequence.

^{\text{∗}}Two sides of the cube SS and TT are called adjacent if they share exactly one edge of the cube. Do note that this condition implies STS \neq T as well.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n31051 \le n \le 3 \cdot 10^5).

The second line of each test case contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1ai61 \le a_i \le 6).

It is guaranteed that the sum of nn over all test cases does not exceed 31053 \cdot 10^5.

Output

For each test case, output the minimum number of operations required to make aa a dice roll sequence.

思路#

由于要保证相邻的两个元素不能在骰子上是“相对”的,所以我们只需要遍历一遍这个序列,然后找相邻的两个元素是否相对,利用

a[i]+a[i1]=7a[i] + a[i - 1] = 7

来进行判断

代码#

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t --) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i ++) cin >> a[i];
int count = 0;
for (int i = 1; i < n; i ++) {
if (a[i] + a[i - 1] == 7 || a[i] == a[i - 1]) {
count ++;
a[i] = 0;
}
}
cout << count << endl;
}
}
Dice-Roll-Sequence
https://github.com/posts/dice-roll-sequence/
作者
FZSGBall
发布于
2026-03-28
许可协议
CC BY-NC-SA 4.0