P1638 逛画展(尺取法)C/C++

在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include <algorithm>
#include<string.h>
#include<math.h>
#define llu unsigned long long
using namespace std;

int main()
{
    
    
	//cout << fixed << setprecision(0);
	//cout << setw(8) << setiosflags(ios::left);
	
	int n,m;
	cin >> n >> m ;
	
	int a[n+5]={
    
    0},count[m+5]={
    
    0};
	
	for(int i=1;i<=n;i++)
	{
    
    
		cin >> a[i] ;
	}
	/*for(int i=1;i<=n;i++)
	{
		cout << a[i] << " " ;
	}*/
	
	int s=0,l=1,r=0,ls=0,rs=1000010;//ls,rs用来存放更新后的结果,也是最终结果
	
	while(r<n)
	{
    
    
		while(r<=n&&s<m)
		{
    
    
			if(!count[a[++r]]) s++;
			count[a[r]]++;
		}
		if(s<m) break;
		
		if( (r-l+1) < (rs-ls+1) ){
    
    
			ls=l;rs=r;
		}
		
		if(--count[a[l++]]==0){
    
    
			s--;
		}
	}
	
	cout << ls << " " << rs << endl ;
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wangyurenwls/article/details/118553196
今日推荐