最长连续不重复序列

链接 : https://www.acwing.com/problem/content/801/

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 1e5 + 7;
int a[N], cnt[N];
    
int main() {
    IO;
    int n, ans = 0;
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0, j = 0; i < n; ++i) {
        cnt[a[i]]++; 
        while (j < i && cnt[a[i]] >= 2) {
             cnt[a[j]]--;
             j++;
        }
        ans = max(ans, i - j + 1);
    }
    cout << ans << '\n'; 
    return 0;
}
posted @ 2021-01-27 20:22  phr2000  阅读(58)  评论(0编辑  收藏  举报