- //双指针
- #include
- #include
- using namespace std;
- using ll = long long;
- const int N = 2e5 + 9;
- int a[N], c[N];
-
- void slove()
- {
- int n; cin >> n;
- for (int i = 1; i <= n; ++i) cin >> a[i];
- //将需要用到的桶重置为0
- for (int i = 1; i <= n; ++i) c[a[i]] = 0;
-
- ll ans = 0;
- for (int i = 1, j = 0; i <= n; ++i)
- {
- //向右寻找j+1
- while (j < n && !c[a[j + 1]]) c[a[++j]]++;
- //更新答案
- ans = max(ans, j - i + 1ll);
- c[a[i]]--;//j+1重复 i向右移动
- }
-
- cout << ans << '\n';
- }
-
-
- int main()
- {
- ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- int t; cin >> t;
- while (t--) slove();
- return 0;
- }