题目描述
Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1,2,…,10000.
Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is ai. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic.
Takahashi can change the color of one slime to any of the 10000 colors by one spell. How many spells are required so that no slimes will start to combine themselves?
Constraints
2≤N≤100
1≤ai≤N
All values in input are integers.
输入
Input is given from Standard Input in the following format:
N
a1 a2 … aN
输出
Print the minimum number of spells required.
样例输入
5
1 1 2 2 2
样例输出
2
提示
For example, if we change the color of the second slime from the left to 4, and the color of the fourth slime to 5, the colors of the slimes will be 1,4,2,5,2, which satisfy the condition.
题目大意就是不能让两个相邻变量相同。
AC代码:
#include <bits/stdc++.h>
using namespace std;
int a[1005];
int main()
{
int n;
int count=0; //用于计需要改变颜色的变量数量
int t=1; //用来计相同相邻的数
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=2;i<=n;i++){
if(a[i]==a[i-1]) t++;
else{
count+=t/2;
t=1; //重置
}
}
count+=t/2; //一直到最后的连续序列
cout<<count;
}