Unique snowflake (Unique snowflakes, ultraviolet A 11572) Sliding Window + set, snowflakes11572

Source: Internet
Author: User

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
    
                                         
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.