3 solutions

  • 2
    @ 2025-7-19 18:59:48
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    int a[N],cnt[N];
    int main()
    {
    	int n;
    	cin>>n;
    	for(int i=1;i<=n;i++)
    	{
    		cin>>a[i];
    	}
    	int res=0;
    	for(int l=1,r=1;r<=n;r++)
    	{
    		cnt[a[r]]++; //增加a[r]的出现次数 
    		while(cnt[a[r]]>1&&l<r) //cnt[a[r]]==1,l-1 cnt[a[r]]==2
    		{
    			cnt[a[l]]--;
    			l++;
    		}
    		res=max(res,r-l+1); //更新以a[r]结尾的最长的不重复区间个数 
    	}
    	cout<<res;
    	return 0;
    }
    /*
    1 		2 		3		 4		 5		 2		 5		 9
    (1,1)   (1,2)   (1,3)    (1,4)   (1,5)   (3,6)   (6,7)
    */
    
    • -1
      @ 2026-8-12 20:58:44
      #include<bits/stdc++.h>
      using namespace std;
      const int N=1e5+10;
      int a[N],cnt[N];
      int main()
      {
      	int n;
      	cin>>n;
      	for(int i=1;i<=n;i++)
      	{
      		cin>>a[i];
      	}
      	int res=0;
      	for(int l=1,r=1;r<=n;r++)
      	{
      		cnt[a[r]]++; //增加a[r]的出现次数 
      		while(cnt[a[r]]>1&&l<r) //cnt[a[r]]==1,l-1 cnt[a[r]]==2
      		{
      			cnt[a[l]]--;
      			l++;
      		}
      		res=max(res,r-l+1); //更新以a[r]结尾的最长的不重复区间个数 
      	}
      	cout<<res;
      	return 0;
      }
      
      
      • -2
        @ 2026-7-30 20:44:49
        #include<bits/stdc++.h>
        using namespace std;
        const int N=1e5+10;
        int a[N],cnt[N];
        int main()
        {
        	int n;
        	cin>>n;
        	for(int i=1;i<=n;i++)
        	{
        		cin>>a[i];
        	}
        	int res=0;
        	for(int l=1,r=1;r<=n;r++)
        	{
        		cnt[a[r]]++;
        		while(cnt[a[r]]>1&&l<r) 
        		{
        			cnt[a[l]]--;
        			l++;
        		}
        		res=max(res,r-l+1);  
        	}
        	cout<<res;
        	return 0;
        }
        • 1

        Information

        ID
        2880
        Time
        1000ms
        Memory
        256MiB
        Difficulty
        3
        Tags
        # Submissions
        46
        Accepted
        14
        Uploaded By