CodeForces - 1090M

本文介绍了一种在给定颜色序列中寻找最长相邻颜色不同区间的算法,通过一次遍历实现O(n)的时间复杂度,适用于多种编程竞赛场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

给一个n,有k种颜色

接着有n个数,每个数代表一个颜色。问你最长相邻颜色不同的区间长度是多长

复杂度 On

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string.h>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
const int maxn =1e5+5;
const int maxm=10000;
const int mod =1e9+7;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
int a[maxn];
int main()
{
    int n,k;scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)scanf("%d",&a[i]);
    int ans=1;int cnt=1;
    for(int i=1;i<n;i++)
    {
        if(a[i]!=a[i-1]) cnt++;
        else cnt=1;
        ans=max(ans,cnt);
    }
    printf("%d\n",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值