2 条题解

  • 3
    @ 2023-12-9 20:51:43
    #include<bits/stdc++.h>
    int n,m,k,s,t,ans;
    int a[100001],sum[100001],first[200001],last[200001];
    int main()
    {
        std::cin>>n;
        memset(first,-1,sizeof(first));
        memset(last,-1,sizeof(last));
        sum[0]=n;
        first[n]=last[n]=0;
        for(int i=1;i<=n;i+=1)
        {
            std::cin>>a[i];
            if(!a[i])a[i]=-1;
            sum[i]=sum[i-1]+a[i];
            if(first[sum[i]]==-1)first[sum[i]]=i;
            last[sum[i]]=i;
        }
        for(int i=0;i<=2*n;i+=1)if(first[i]!=-1)ans=std::max(ans,last[i]-first[i]);
        std::cout<<ans;
    }
    
    • 2
      @ 2023-12-2 10:50:07
      #include <bits/stdc++.h>
      using namespace std;
      int n, a[100005], sum[100005], first[200005], last[200005], ans;
      int main()
      {
          cin >> n;
          memset(first, -1, sizeof(first));
          memset(last, -1, sizeof(last));
          sum[0] = n;
          first[n] = last[n] = 0;
          for (int i = 1; i <= n; i++)
          {
              cin >> a[i];
              if (!a[i])
              {
                  a[i] = -1;
              }
              sum[i] = sum[i - 1] + a[i];
              if (first[sum[i]] == -1)
              {
                  first[sum[i]] = i;
              }
              last[sum[i]] = i;
          }
          for (int i = 0; i <= 2 * n; i++)
          {
              if (first[i] != -1)
              {
                  ans = max(ans, last[i] - first[i]);
              }
          }
          cout << ans;
          return 0;
      }
      
      • 1

      信息

      ID
      585
      时间
      1000ms
      内存
      128MiB
      难度
      5
      标签
      递交数
      219
      已通过
      84
      上传者