题目链接:https://codeforces.com/contest/1090/problem/M

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.

题意:

路边有 $n$ 座房子,每座房子有可能涂上 $k$ 种颜色中的一种。Anya不喜欢相邻两座房子有相同颜色,因此要选择路上最长的一段,使得这一段上所有相邻的房子都满足颜色不同。

题解:

$O(n)$ 维护当前段长度和最长长度即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int n,k,a[maxn];
int main()
{
cin>>n>>k;
int res=;
for(int i=,len;i<=n;i++)
{
scanf("%d",&a[i]);
if(i== || a[i]==a[i-]) len=;
else len++;
res=max(len,res);
}
cout<<res<<endl;
}

最新文章

  1. neatbean 8+版本 配置
  2. A - Ice_cream’s world III
  3. Maximo子表中增加附件功能
  4. iOS6.0下获取通讯录用户列表
  5. 简明python教程 --C++程序员的视角(二):函数及作用域
  6. 求单链表倒数第m个结点
  7. Operfire/XMPP
  8. HTML5 自适应rem布局
  9. IOS 面试 --- 动画 block
  10. 使用jquery插件uploadify上传文件的方法与疑问
  11. 在一个apk中调用另外一个apk中的activity
  12. Leetcode 197. Rising Temperature
  13. Actor-ES框架:Ray
  14. Java并发系列[8]----CyclicBarrier源码分析
  15. NameNode与DataNode的工作原理剖析
  16. Codeforces Round #505 Div. 1 + Div. 2
  17. linux操作系统中的netstat命令查看端口状态的使用和window操作系统查看端口号
  18. ORA-02275: 此表中已经存在这样的引用约束条件
  19. Java 8 停止维护,Java 9 难产,IDEA 2018 发布,还有……
  20. js 时间戳转特定格式的日期

热门文章

  1. emacs 集成astyle
  2. Nop--NopCommerce源码架构详解专题目录
  3. java 定时任务之一 @Scheduled注解(第一种方法)
  4. 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
  5. vue-router2.x使用入门
  6. CountDownLatch、CyclicBarrier和Semaphore 使用示例及原理
  7. input文件上传(上传单个文件/多选文件/文件夹、拖拽上传、分片上传)
  8. fiddler抓包url有乱码
  9. java通过jdbc访问mysql,update数据返回值的思考
  10. Quartz.net设置任务中同时最多运行一个实例 [DisallowConcurrentExecution]