!-- Loading 底层遮罩 -->

UVA11572 UniqueSnowflakes

感谢所有AC

 

传送门

思路

       尺取+map桶。用桶保存数字出现的次数,次数小于等于1时右指针持续推进,当次数大于1时左指针推进,推进的同时对左指针指向的数字的次数减一,直到右指针指的数字次数从2变成1再停止,序列长度即为右指针-左指针。

代码

#include<iostream>
#include<algorithm>
#include<map>
#define MAXN 1000007
using namespace std;
int a[MAXN], T, n;
map<int, int> cnt;
void work() {
    int ans = 1, l = 1, r = 2;
    cnt.clear();
    cnt[a[l]]++, cnt[a[r]]++;
    for (; r <= n;) {
        while (cnt[a[r]] > 1) cnt[a[l++]]--;
        ans = max(ans, r - l + 1);
        cnt[a[++r]]++;
    }
    cout << ans << endl;
}
int main(void)
{
    cin >> T;
    for (int i = 1; i <= T; i++)
    {
        cin >> n;
        for (int j = 1; j <= n; j++)
            cin >> a[j];
        work();
    }
    return 0;
}
 

 

posted @ 2022-03-30 21:56  Thinker-X  阅读(10)  评论(0)    收藏  举报