AtCoder Grand Contest 026-Colorful Slimes 2

题目描述

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值