### 【高效算法设计——滑动窗口】UVa 11572 Unique Snowflakes

`#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <set>using namespace std;set<int>M;int w[1000000+50];int main(){	int n,i,j,ans,l,r,T;	scanf("%d",&T);	while(T--)    {        scanf("%d",&n);        for(i=1;i<=n;i++)            scanf("%d",&w[i]);        ans=0;        M.clear();        l=1;        r=1;        ans=1;        for(;r<=n;)        {            while(!M.count(w[r]) && r<=n)            {                M.insert(w[r]);                r++;            }            ans=max(r-l,ans);            M.erase(w[l]);            l++;        }        printf("%d\n",ans);    }	return 0;}`

`#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <set>using namespace std;set<int>M;int w[1000000+50];int main(){	int n,i,j,ans,l,r,T;	scanf("%d",&T);	while(T--)    {        scanf("%d",&n);        for(i=1;i<=n;i++)            scanf("%d",&w[i]);        ans=0;        M.clear();        l=1;        r=1;        ans=1;        for(;r<=n;)        {            while(!M.count(w[r]) && r<=n)            {                M.insert(w[r]);                r++;            }            ans=max(r-l,ans);            M.erase(w[l]);            l++;        }        printf("%d\n",ans);    }	return 0;}`

#### 注意！

© 2014-2022 ITdaan.com 联系我们：