尺取

引言

尺取是一种很有意思的算法,思想也很简单(我为什么现在才会
这玩意主要是和一些算法搞结合,像二分一样,很基础很巧妙也很实用。

例题

贴一道简单题,也可当作模板,但实际上尺取变化很多,要灵活使用
UVA 11572

#include<bits/stdc++.h>
#define int long long 
const int Max=1e6+10;
using namespace std;

int t,n,res;
int a[Max];
map<int,int>cnt;

signed main()
{
	freopen("in.txt","r",stdin);
	ios::sync_with_stdio(0);
	cin>>t;
	while(t--)
	{
		res=1;
		cnt.clear();
		cin>>n;
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int l=1,r=0;l<=n;)
		{
			while(r<n) 
			{
				r++;
				if(cnt[a[r]]==1) {r--;break;}
				cnt[a[r]]=1;
			}
			res=max(res,r-l+1); 
			cnt[a[l]]=0;
			l++;
		}
		cout<<res<<endl;
	}
	return 0;
} 
posted @ 2022-01-01 12:49  juuich  阅读(30)  评论(0编辑  收藏  举报