比赛 EYOI暨SBOI暑假快乐赛6th 评测结果 AAAAAAAAAA
题目名称 Count 1s 最终得分 100
用户昵称 op_组撒头屯 运行时间 0.039 s
代码语言 C++ 内存使用 3.25 MiB
提交时间 2022-06-30 11:23:16
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N=200000+5;
int n,a[N],ans=0;
int l,r,now=0,nowl=1,mx=0,mn=0;
int sum0=0,sum1=0;
int main(){
    freopen ("count1s.in","r",stdin);
    freopen ("count1s.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if (a[i]==0){
            now++;sum0++;
        }
        else{
            now--;sum1++;
        } 
        if (now<0){
            now=0;nowl=i+1;
            continue;
        }
        if (now>mx){
            l=nowl;r=i;
            mx=now;
        }
    }
    mx+=sum1;
    //cout<<l<<" "<<r<<" "<<mx<<endl;
    now=nowl=l=r=0;
    for (int i=1;i<=n;i++){
        if (a[i]==1)now++;
        else now--;
        if (now<0){
            now=0;nowl=i+1;
            continue;
        }
        if (now>mn){
            l=nowl;r=i;
            mn=now;
        }
    }
    mn=sum1-mn;
    //cout<<l<<" "<<r<<" "<<mn<<endl;
    printf("%d\n",mx-mn+1);
    return 0;
}