1 条题解
-
0
思路
维护最长无重复连续区间,左界只能向右;标程离散化加上次位置,避免百万哈希项内存风险。
参考程序(C++11)
#include <bits/stdc++.h> using namespace std; int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;cin>>n;vector<int>a(n);for(int &x:a)cin>>x;vector<int>v=a;sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());vector<int>last(v.size(),-1);int left=0,ans=0;for(int i=0;i<n;i++){int id=lower_bound(v.begin(),v.end(),a[i])-v.begin();left=max(left,last[id]+1);ans=max(ans,i-left+1);last[id]=i;}cout<<ans<<'\n';}
- 1