题面
Consider the following cube where numbers and lie on opposite sides:
A sequence of integers from to is called a dice roll sequence if it satisfies the following condition:
- All pairs of adjacent elements lie on adjacent sides of the cube.
For example, is a dice roll sequence, while is not because and are not on adjacent sides of the dice. Additionally, is not a dice roll sequence because and are on the same (not adjacent) side of the dice.
Given a sequence of integers from to , you can perform the following operation any number of times (possibly zero).
- Select an index and an integer . Then, change the value of to .
Please determine the minimum number of operations required to make a dice roll sequence.
Two sides of the cube and are called adjacent if they share exactly one edge of the cube. Do note that this condition implies as well.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer ().
The second line of each test case contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output the minimum number of operations required to make a dice roll sequence.
思路
由于要保证相邻的两个元素不能在骰子上是“相对”的,所以我们只需要遍历一遍这个序列,然后找相邻的两个元素是否相对,利用
来进行判断
代码
#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; }}