CF 1141 - B Maximal Continuous Rest

题目: 传送门
思路: 即求最长的 1 连续子串,可以DP。
因为数据范围不大,当时我直接考虑所有情况暴力了。

#include <iostream>
#include <algorithm>
using namespace std;

int a[200010];

int main() {
    int n;
    cin>>n;
    for(int i=0;i<n;i++) {
        cin>>a[i];
    }
    int i=0;
    int before=0,behind=0;
    if(a[0]==1) {
        before = 0;
        for(i=0;i<n;i++) {
            if(a[i]==1) before++;
            else {
                break;
            }
        }
    }
    int j=n-1;
    if(a[n-1]==1) {
        behind = 0;
        for(j=n-1;j>=0;j--) {
            if(a[j]==1) behind++;
            else {
                break;
            }
        }
    }
    int ans = 0;
    int now = 0;
    for(;i<=j;i++) {
        if(a[i]==0) {
            ans = max(now,ans);
            now=0;
        }
        else {
            now++;
        }
    }
    //cout<<behind<<' '<<before<<' '<<ans<<endl;
    cout<<max(behind+before,ans)<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43305984/article/details/89206525
今日推荐