样例输出1 7请大佬debug
查看原帖
样例输出1 7请大佬debug
920938
XingChen_MoNian楼主2024/2/25 16:24
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e6+10;
int a[N],flag[N];
int main(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	int l=1,cnt=0,tot=0x7FFFFFFF,ansl,ansr;
	for(int r=1;r<=n;r++){
		if(!flag[a[r]]){
			flag[a[r]]++;
			cnt++;
		}
		while(flag[a[l]]>1){
			flag[a[l]]--;
			l++;
		}
		if(cnt==m){
			if(r-l+1<tot){
				tot=r-l+1;
				ansl=l;
				ansr=r;
			}
		}
	}
	cout<<ansl<<" "<<ansr; 
//  fclose(stdin);
//  fclose(stdout);
	return 0;
}
/*

*/

2024/2/25 16:24
加载中...