一定不要看错范围(悲)-抽象双指针
查看原帖
一定不要看错范围(悲)-抽象双指针
575053
Ningyan7927楼主2023/8/7 10:35

非常抽象的双指针,

一开始一直冒re,后面看了两年半才发现N是1000000不是1000,范围开小了(悲)。

#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
int n,m,a[1000005],b[1000005],start=1,flag=0,chixi=0,small,front,behind;
using namespace std;
int main() {
	cin>>n>>m;
	int end=m;
	for(int i=1; i<=n; i++) {
		cin>>a[i];
	}
//++math
	for(int i=start; i<=end; i++) {
		b[a[i]]++;
	}
//——————fight——————
//behind
	while(flag==0) {
		for(int i=1; i<=m; i++) {
			if(b[i]>0)flag=1;
			else {
				flag=0;
				end++;
				b[a[end]]++;
				break;
			}
		}
	}
//front
	while(true) {
		if(b[a[start]]>1) {
			b[a[start]]--;
			start++;
		} else break;
	}
//have
	small=end-start;
	front=start;
	behind=end;
//fight2
	while(end!=n) {
		end++;
		b[a[end]]++;
		while(true) {
			if(b[a[start]]>1) {
				b[a[start]]--;
				start++;
			} else break;
		}
		if(end-start<small) {
			small=end-start;
			front=start;
			behind=end;
		}
	}
	cout<<front<<" "<<behind;
	return 0;
}
//12 5
//2 5 3 1 3 2 4 1 1 5 4 3
//12 5
//2 5 3 1 3 2 4 1 2 3 4 5

蒟蒻水平的散装英语

2023/8/7 10:35
加载中...