UVA 11572 the only snowflake
Test instructions: give you an array from 1 to N, ask for the longest consecutive non-repeating subsequence, classic sliding window problem, the method is to maintain a window, set the left and Right boxes, and then continue to maintain and update
Method One:
#include"Iostream"#include"Set"#include"CString"#include"Cstdio"#include"Algorithm"UsingNamespace Std;Constint MAXN=1000000+10; set<Int>book;int a[MAXN];IntMain(){int T; Cin>>t;While(T--){int nT, Sum=0, ans=0; Cin>>n; Book.Clear();For(int I=0; I<n; I++){cin>>a[I];}int R=0, l=0;For(; r<n;){While(!book.Count(A[R]) &&r<n){Book.insert (A[r]) ; R++; Ans=max ( Ans,r-lerase (A[l++; Cout<<ans<<endl return 0;< Span class= "Sh-cbracket" >}
Method Two:
#include"Iostream"#include"Set"#include"CString"#include"Cstdio"#include"Algorithm"UsingNamespace Std;Constint MAXN=1000000+10;int POS[MAXN];int a[MAXN];IntMain(){int T; Cin>>t;While(T--){int nT, Sum=0, ans=0; Cin>>n;Memset(POS,-1,sizeof(POS));For(int I=0; I<n; I++){cin>>a[I];}int R=0, l=0; A[n]=a[n-1];For(int I=0; I<=n; I++){If(POS[A[I]]>=l {ans=max< Span class= "Sh-symbol" > (Ans,i-l=pos[a[i]]+1 Pos[a[i]]=i Cout<<ans<<endl return 0;< Span class= "Sh-cbracket" >
Fourth week of training (efficient algorithm design) k problem (sliding window)