1 solutions

  • 0
    @ 2025-11-5 16:06:25

    C++ :

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    
    int n,m;
    int sum=0,tot=0x7fffffff;
    int g[1000001]={0};
    int h[1000001]={0};
    bool f[1000001]={0};
    
    void init();
    void work();
    int main()
    {
    	init();
    	if(m==n)
    	{
    		printf("%d",m);
    	}
    	else
    	{
    		work();
    	}
    	return 0;
    }
    
    void init()
    {
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=n;++i)
    	{
    		scanf("%d",&h[i]);
    	}
    }
    
    void work()
    {
    	int s=1,e=1,w=0;
    	sum=0;
    	while(e<=n)
    	{
    		while(sum<m)
    		{
    			w++;
    			g[h[w]]=w;
    			if(!f[h[w]])
    			{
    				f[h[w]]=1;
    				sum++;
    			}
    			if(w>n)
    			{
    				break;
    			}
    		}
    		e=w;
    		if(e-s+1<tot)
    		{
    			tot=e-s+1;
    		}
    		if(g[h[s]]==s)
    		{
    			f[h[s]]=0;
    			sum--;
    		}
    		s++;
    	}
    	printf("%d",tot);
    }
    
    • 1

    Information

    ID
    17484
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By