Nowcoder Hash Function (topological sorting && segment tree optimization)

Source: Internet
Author: User

Topic links

Test Instructions: give a hash table, its way to avoid the collision is linear detection and re-hash, now ask you whether the hash table is valid, if it is legal to output all the elements in the order of insertion, if there is more than the output dictionary of the smallest sequence, if not legal output-1

Analysis:

Through the simulation and observation of the sample, we can find

If an element a is supposed to go to the position pos is occupied by element B, then B is placed before a

Since the linear re-probing is used, all positions in the Posnow from Pos to the present position should be placed before a

For example 4 8 0-1 in this hash table of length 4 The 0 element should be in the 0 position, but see 4 in this position so 4 is inserted before 0

And then go forward to find the 0 elements of the current position, there is a 8 in, so 8 also one before 0 insert

This gives us some revelation, for something to be done before another thing

Then naturally think of building a precursor diagram, and then topological sequencing, if the topological sequencing success indicates that there is a solution, otherwise no solution (existence ring)

Require the smallest dictionary order, use queue to build topology sorting queue to be changed to priority queue

But there is another problem, and you will actually find that each build, is a number of intervals connected to a node

Such Jiantu, Edge will become very much, such as this data 1024, 512, 256, 128, 64, 32, 16, 8 all of the hash value is 0

So the edge will be more and more, close to the n ^ 2 build, build the complexity of the map has been unbearable

So we need an optimization, that is, the optimization of the segment tree map

Suitable for interval-to-interval and interval-to-point edges

First, the line tree, length 1 ~ n

According to the meaning of the application of segment tree in this problem

Initialize each node to connect one edge to its father

Indicates that the child node belongs to its father, and this is obvious.

The original array elements are then assigned to the leaf nodes of the segment tree.

Each interval to a number of connected edges can be changed to some of the segment tree nodes representing this interval are connected to the leaf nodes representing this number

Finally, the completion of the predecessor map (that is, each node in the original segment tree to connect the corresponding edge) to run the topology sort can be

#include <bits/stdc++.h>#defineLL Long Long#defineULL unsigned long Long#defineSCL (i) scanf ("%lld", &i)#defineSCLL (i, J) scanf ("%lld%lld", &i, &j)#defineSclll (I, J, K) scanf ("%lld%lld%lld", &i, &j, &k)#defineScllll (I, J, K, L) scanf ("%lld%lld%lld%lld", &i, &j, &k, &l)#defineSCS (i) scanf ("%s", I)#defineSCI (i) scanf ("%d", &i)#defineSCD (i) scanf ("%lf", &i)#defineScIl (i) scanf ("%i64d", &i)#defineScii (i, J) scanf ("%d%d", &i, &j)#defineSCDD (i, J) scanf ("%lf%lf", &i, &j)#defineScill (i, J) scanf ("%i64d%i64d", &i, &j)#defineSCIII (I, J, K) scanf ("%d%d%d", &i, &j, &k)#defineSCDDD (I, J, K) scanf ("%lf%lf%lf", &i, &j, &k)#defineScilll (I, J, K) scanf ("%i64d%i64d%i64d", &i, &j, &k)#defineSCIIII (I, J, K, L) scanf ("%d%d%d", &i, &j, &k, &l)#defineSCDDDD (I, J, K, L) scanf ("%lf%lf%lf%lf", &i, &j, &k, &l)#defineScillll (I, J, K, L) scanf ("%i64d%i64d%i64d%i64d", &i, &j, &k, &l)#defineLson L, M, rt<<1#defineRson m+1, R, rt<<1|1#defineLowbit (i) (I & (i))#defineMem (i, J) memset (i, J, sizeof (i))#defineFIR First#defineSEC Second#defineVI vector<int>#defineINS (i) Insert (i)#definePB (i) push_back (i)#definePII Pair<int, int>#defineVL Vector<long long>#defineMK (i, J) Make_pair (I, J)#defineAll (i) I.begin (), I.end ()#definePLL Pair<long Long, long long>#define_time 0#define_input 0#define_output 0clock_t START, END;void__sttime ();void__entime ();void__ioput ();using namespacestd;Const intMAXN = 2e5 +Ten; VI ans;intPRESUM[MAXN];intARR[MAXN];intNODE[MAXN];intin[maxn<<2];intpos[maxn<<2];structedge{intV, NXT; }edge[maxn<<4];inthead[maxn<<2], Edgecnt;inlinevoidInitintN)    {ans.clear ();  for(intI=0; I<= (n<<2); i++) {In[i]=0; Head[i]= -1; } edgecnt=0;} InlinevoidAddedge (int  from,intTo ) {EDGE[EDGECNT].V=to ; EDGE[EDGECNT].NXT= head[ from]; head[ from] = edgecnt++; In[to]++;}voidBuild (intLintRintRT) {    if(L = =R)        {SCI (arr[l]); PRESUM[L]= presum[l-1] + (arr[l] = =-1 ); NODE[L]=RT; POS[RT]=l; return ; } Pos[rt]=0; Addedge (Rt<<1|1, RT); Addedge (Rt<<1, RT); intm = (L + r) >>1;    Build (Lson); Build (Rson);}voidConnect (intLintRintCintLintRintRT) {    if(l <= l && R <=R)        {Addedge (RT, C); return ; }    intm = (L + r) >>1; if(L <=m) Connect (L, R, C, Lson); if(M <r) Connect (L, R, C, Rson);}BOOLTopo_sort (intN) {Priority_queue<pii, Vector<pii>, greater<pii> >que;  for(intI=1; i<=n; i++){        if(In[node[i]] = =0) {Que.push (MK (Arr[i], node[i])); }    }     while(!Que.empty ()) {PII top=que.top (); Que.pop (); if(Top.fir! =-1) ANS.PB (Top.fir);  for(intI=HEAD[TOP.SEC]; i!=-1; I=edge[i].nxt) {            intEiv =edge[i].v; if(--in[eiv] = =0){                if(Pos[eiv] >0) Que.push (MK (Arr[pos[eiv]], Eiv)); ElseQue.push (MK (-1, Eiv)); }        }    }    return((int) ans.size () = = N-presum[n]);}intMainvoid) {__sttime (); __ioput (); intncase;    SCI (ncase);  while(ncase--){        intN;        SCI (n);        Init (n); Build (1N1); if(Presum[n] = = N) {Puts ("");Continue; } BOOLOK =true;  for(intI=1; i<=n; i++){            if(Arr[i] = =-1)Continue; if((arr[i]%n +1) = = i)Continue; intL = arr[i]%n +1, R = (I-1) ==0? N:I-1; if(L <=R) {                if(Presum[r]-presum[l-1] >0) {OK =false; Break; } Connect (L, R, Node[i],1N1); }Else{                if(Presum[n]-presum[l-1] + Presum[r] >0) {OK =false; Break; } Connect (L, N, Node[i],1N1); Connect (1, R, Node[i],1N1); }        }        if(!ok) puts ("-1"); Else{            if(!topo_sort (n)) puts ("-1"); Else  for(intI=0; i< (int) Ans.size (); i++) printf ("%d", Ans[i]); Puts""); }}__entime ();return 0;}void__sttime () {#if_timeSTART=clock (); #endif}void__entime () {#if_timeEND=clock (); Cerr<<"Execute time ="<< (Double) (End-start)/clocks_per_sec<<Endl; #endif}void__ioput () {#if_inputFreopen ("In.txt","R", stdin); #endif    #if_outputFreopen ("OUT.txt","W", stdout); #endif}
View Code

Nowcoder Hash Function (topological sorting && segment tree optimization)

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.