Unique snowflake (Unique snowflakes, ultraviolet A 11572) Sliding Window + set, snowflakes11572
Enter A sequence A with the length of n (n <= 10 ^ 6) and find A continuous subsequence AL ~ AR, so that the sequence does not have the same elements.
Analysis: assume that the sequence element is numbered from 0, the left endpoint of the continuous subsequence is L, and the right endpoint is R. Consider starting point L = 0 first. Increasing R from R = 0 is equivalent to extending the right endpoint of the desired sequence to the right. When A [R + 1] cannot be extended ~ In R]), you only need to increase L and extend R. Since the current A [L ~ R] is a feasible solution. After L increases, it must be a feasible solution. Therefore, you do not need to decrease R. continue to increase.
Finally, consider the section "determining whether it can be extended. One easy way to think of is to use STL sets to save A [L ~ The set of elements in R]. When R increases, judge whether A [R + 1] appears in set, when R is added to 1, A [R + 1] is inserted into the set. When L is added to 1, A [L] is removed from the set Zhongshan coarse. Because the insertion, deletion, and query of set are O (logn), the time complexity of this algorithm is O (nlogn ). The Code is as follows:
#include
#include
using namespace std;const int maxn = 1000000 + 5;int A[maxn];int main(){ int T,n; cin>>T; while(T--){ cin>>n; for(int i = 0; i < n; i++){cin>>A[i]; } set
s; int L = 0, R = 0,ans = 0; while(R < n){ while(R