UVA11572-Unique Snowflakes-(最长不同连续子序列)

题意:给n个数,求最长不同连续子序列。n<=1e6。

解题过程:

1.记录数据存于数组

2.用左右指针l和r指向这段连续区间

3.右指针往右走,如果遇到没有存在于set集合的数就插入集合

否则左指针往右走,逐渐删去set里的数据,不断更新答案,一直保留最大值。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<set>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
#define ll long long

int a[1000086];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,l,r,maxx;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        set<int>se;
        l=r=maxx=0;
        while(r<n)
        {
            while(r<n && !se.count( a[r] ) )///r<n也需要,否则可能r自增加过头了
                se.insert(a[r++]);
            maxx=max(maxx,r-l);///r目前是冒过头一位的,不需要r-l+1
            se.erase(a[l++]);
        }
        printf("%d\n",maxx);
    }
    return 0;
}

 

posted @ 2019-03-10 16:27  守林鸟  阅读(145)  评论(0编辑  收藏  举报