You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
671 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}