Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstdio>
- #include<algorithm>
- #include<set>
- #define LL long long
- using namespace std;
- int N;
- int in[50010];
- long long hash[50010];
- LL base=31;
- long long modulo=9223372036854775807;
- bool step(int k)
- {
- set<LL> sset;
- if(k==1)
- {
- for(int i=0;i<N;i++)
- {
- if(sset.find(in[i])!=sset.end()) return false;
- sset.insert(in[i]);
- }
- return true;
- }
- //init hash
- LL ihash=0;
- LL d=base;
- for(int i=0;i<k;i++)
- {
- ihash=(base) * (ihash) + in[i];
- ihash=ihash%modulo;
- if(i<k-2) d=d*base;
- d=d%modulo;
- }
- hash[0]=ihash;
- sset.insert(ihash);
- //printf("i=%lld d %lld\n",ihash,d);
- for(int i=1;i<=N-k;i++)
- {
- hash[i]=hash[i-1]-d*in[i-1];
- hash[i]=hash[i]*base;
- hash[i]=hash[i]+in[i+k-1];
- //printf("H[%d]=%lld\n",i,hash[i]);
- if(sset.find(hash[i])!=sset.end()) return false;
- sset.insert(hash[i]);
- }
- return true;
- }
- int main()
- {
- int Z;
- scanf("%d",&Z);
- while(Z--)
- {
- scanf("%d",&N);
- for(int i=0;i<N;i++)
- scanf("%d",&in[i]);
- /* for(int i=0;i<N;i++)
- {
- printf("step(%d)=%d\n",i,step(i));
- }*/
- int p=0, q=N,s;
- while(p!=q)
- {
- s=(p+q+1)/2;
- //printf("%d\n",s);
- if(step(s))
- q=s-1;
- else
- p=s;
- }
- if(s==N) printf("%d\n",N); else
- printf("%d\n",s+1);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment