Greedy
Well...... In fact, generating this matrix is a $o (n^2) $ simulation = =
And then? Minimum dictionary order? Greedy Bai = = can choose 1 to choose 1, then can choose 2 to choose 2 ...
We found that for the Matrix (N,M), assuming that the position of 1 is (x, y), then after we have chosen 1, the optional range becomes: (x-y) & (x, y) ~ (n,m), that is, a matrix is split into four pieces, We can choose recursively in the upper left and right two blocks ...
Then each time we choose, the new optional Range in fact violence O (n) Maintenance is possible, because we only select $o (n) $ times, the complexity of each maintenance is $o (n) $, total complexity or $o (n^2) $
As for the card space problem ... Since the opening of a 5000*5000 int is 100M, so I started to open a T array first calculated, and then generate MAP[I][J], this practice is going to explode ... (because there is a pos[i], save I this number of coordinates, this can be used short, need 100M)
So improved: Do not generate a T array, directly on the map above, it is possible = =
1 /**************************************************************2 problem:36713 User:tunix4 language:c++5 result:accepted6 time:29944 Ms7 memory:197448 KB8 ****************************************************************/9 Ten //Bzoj 3671 One#include <vector> A#include <cstdio> -#include <cstring> -#include <cstdlib> the#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) + #defineD (i,j,n) for (int i=j;i>=n;--i) - #definePB Push_back + using namespacestd; AInlineintGetint () { at intv=0, sign=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*Sign ; - } - Const intn=5010, inf=~0u>>2; intypedefLong LongLL; - /******************tamplate*********************/ to + intN,m,q,mp[n][n],l[n],r[n]; - intx0,a,b,c,d; theInlineintRan () {returnx0= (LL) x0*x0*a+ (LL) x0*b+c)%D;} *typedef pair< Short, Short>PII; $ #defineMk Make_pairPanax NotoginsengPII pos[n*N]; - intans[n+n],cnt; the + intMain () { A #ifndef Online_judge theFreopen ("3671.in","R", stdin); +Freopen ("3671.out","W", stdout); - #endif $X0=getint (), A=getint (), B=getint (), C=getint (), d=getint (); $N=getint (); M=getint (); q=getint (); -F (I,1, N) F (J,1, m) mp[i][j]= (i-1) *m+J; -F (I,1Nm) { the intx1=i/m+1, Y1=i%m,t=ran ()%i+1, x2=t/m+1, y2=t%m; - if(!y1) X1--, y1=m;Wuyi if(!y2) X2--, y2=m; the swap (mp[x1][y1],mp[x2][y2]); - } Wu intx, y; -F (I,1, Q) { AboutX=getint (), y=getint (); $ intx1=x/m+1, y1=x%m,x2=y/m+1, y2=y%m; - if(!y1) X1--, y1=m; - if(!y2) X2--, y2=m; - swap (mp[x1][y1],mp[x2][y2]); A } +F (I,1, N) F (J,1, M) { thePOS[MP[I][J]]=MK (( Short) I, ( Short) j); - } $ theF (I,1, N) l[i]=1, r[i]=m; theF (I,1Nm) { the if(cnt==n+m-1) Break; the intx=pos[i].first,y=Pos[i].second; - //printf ("pos[%d]= (%d,%d) \ n", i,x,y); in if(L[x]<=y && r[x]>=y) { theans[++cnt]=i; theF (I,1, X-1) r[i]=min (y,r[i]); AboutF (i,x+1, N) l[i]=Max (y,l[i]); the } the //F (i,1,n) printf ("l[%d]=%d r[%d]=%d\n", I,l[i],i,r[i]); the //puts (""); + } -F (I,1, cnt-1) printf ("%d", Ans[i]); theprintf"%d", ans[cnt]);Bayi return 0; the}
View Code 3671: [Noi2014] random number generator time limit:50 Sec Memory limit:256 MB
submit:774 solved:374
[Submit] [Status] [Discuss] Description
Input
The 1th line contains 5 integers, followed by x_0,a,b,c,d, describing the random seed required by the random number generation algorithm used by small H. The 2nd line contains three integer n,m,q, indicating that the small h would like to generate a 1 to NXM arrangement to fill in her N rows M-column of the chessboard, and the small h after the initial NXM exchange operation, and then a Q-time additional switching operations. Next Q line, line I contains two integer u_i,v_i, indicating that the extra swap operation will exchange values of T_ (u_i) and T_ (v_i).
Output
The output line contains a n+m-1 of positive integers separated by spaces, representing the smallest sequence of paths that can be obtained by the dictionary order.
Sample INPUT1 3 5 1 71
3 4 3
1 7
9 9
6 {
Sample Output
1 2 6 8 9 12
HINT
The space limit for the subject is a maximum of MB, and it is important to ensure that the total memory space used by the submitted code runs does not exceed this limit.
A 32-bit integer, such as longint in C + + in int and Pascal, is 4 bytes, so if you declare an array of 32-bit integer variables of length 1024x1024 in your program, you will consume 4 MB of memory space.
2≤n,m≤5000
0≤q≤50000
0≤a≤300
0≤b,c≤108
0≤x0<d≤108
1≤ui,vi≤nxm
Source [Submit] [Status] [Discuss]
"Bzoj" "3671" "NOI2014" random number generator