1 solutions

  • 0
    @ 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)
    */
    

    Information

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