最长平台

GitHub跳转原题关系图返回列表

题意与原解析均从本地 OpenJudge 缓存迁移。

OJ: noi_openjudge

题目 ID: ch0109-12

难度:未知

标签:数组连续段python

日期: 2026-07-30 23:01

题意

完整题面见同目录的 problem.md

思路

扫描有序数组的相同连续段,维护当前平台和最长平台长度。

代码

Python代码

python
count = int(input())
numbers = list(map(int, input().split()))
longest = current = 1
for index in range(1, count):
    current = current + 1 if numbers[index] == numbers[index - 1] else 1
    longest = max(longest, current)
print(longest)

C++代码

cpp
#include <cstdio>
#include <cstring>

int n;
int s[2000];
int main(){
    scanf("%d",&n);
    int i,cnt=1,max=1;
    for (i=1;i<=n;i++){
        scanf("%d",&s[i]);
    }
    for (i=2;i<=n;i++){
        if(s[i] == s[i-1]){
            cnt++;
        }
        else {
            if( max < cnt) max = cnt;
            cnt = 1;
        }
    }
    if( max < cnt) max = cnt;
    printf("%d\n",max);
    return 0;
}

复杂度

总结