題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2619
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <bits/stdc++.h> using namespace std;
int main(){ int kase; cin >> kase; int n; while(kase--){ cin >> n; vector<int> v(n); for(int i=0; i<n; ++i) cin >> v[i]; map<int, int> mp; int cur; int max_len = 0; for(int r=0, l=0; r<n; ++r){ cur = v[r]; if(mp.count(cur) && mp[cur] >= l){ l = mp[cur]+1; } mp[cur] = r; max_len = max(max_len, r-l+1); } cout << max_len << '\n'; } return 0; }
|