CF M. The Pleasant Walk

M. The Pleasant Walk

time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
There are n houses along the road where Anya lives, each one is painted in one of k possible colors.

Anya likes walking along this road, but she doesn’t like when two adjacent houses at the road have the same color. She wants to select a long segment of the road such that no two adjacent houses have the same color.

Help Anya find the longest segment with this property.

Input
The first line contains two integers n and k — the number of houses and the number of colors (1≤n≤100000, 1≤k≤100000).

The next line contains n integers a1,a2,…,an — the colors of the houses along the road (1≤ai≤k).

Output
Output a single integer — the maximum number of houses on the road segment having no two adjacent houses of the same color.

Example
input
8 3
1 2 3 3 2 1 2 2
output
4
Note
In the example, the longest segment without neighboring houses of the same color is from the house 4 to the house 7. The colors of the houses are [3,2,1,2] and its length is 4 houses.

瞎胡翻译:找一条没有相邻相同数字的最长子串的长度
蜜汁思路:每个位置记录一下当前位置的数num以及当前位置的最大值max,每个位置的max默认为1,比较当前位和前一位的num是否相同,相同,当前位就不能和前一位组成序列,不相同,就可以组成序列,所以要更新max

#include<stdio.h>
struct p{
	int num;
	int max;
};
int main()
{
	struct p a[100005];
	int n,k;
	int i;
	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++){
		scanf("%d",&a[i].num);
		a[i].max = 1;
		if(i==0){
			continue;
		}
		else{
			if(a[i-1].num != a[i].num){
				a[i].max = a[i-1].max + 1;
			}
		}
	}
	int j=0;
	for(i=0;i<n;i++){
		if(a[i].max>a[j].max){
			j=i;
		}
	}
	printf("%d",a[j].max);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/D_mengxin/article/details/84950079