Files
python/TangDou/AcWing/DoublePointer/799.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

22 lines
692 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N]; // 原数组
int b[N]; // 记个数的桶
int res;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1, j = 1; i <= n; i++) {
b[a[i]]++; // a[i]这个数在桶中的个数+1
while (b[a[i]] > 1) b[a[j++]]--; // 如果因为a[i]的加入,导致桶中a[i]的数量大于1
// 也就是有了重复,则需要从窗口左侧尝试去掉一个,看看情况会不会变好
// 不断刷新滑动容器长度
res = max(res, i - j + 1);
}
printf("%d\n", res);
return 0;
}