Question :..... Take a long look.
First, since the Lexicographic Order of the sorted sequence is the smallest, You must select as few numbers as possible.
Then T is 1 ~ The M * n sequence does not exist. (At the beginning, I missed this condition)
Okay, this question is greedy.
Select the minimum point that is not marked each time, and mark both the lower left and the upper right corner (remember to mark the duplicate break, otherwise it will be down)
Note that if two int values are opened for the 5000*5000 array, the limit is increased. Therefore, the T array must be reused.
The Code ran for 38 seconds. However, the area that can be selected for each row must be continuous. You can maintain one L and one R for each row and get the most value for each update. This Code ran for 28 seconds. slow Qaq
# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # define M 5010 using namespace STD; typedef long ll; int T [M * m], map [m] [m], L [m], R [m]; ll seed, a, B, c, d; int N, m, Q; inline int rand () {return seed = (A * seed % d + B * seed % d + C) % d;} void kill (INT X, int y) {int I; for (I = 1; I <X; I ++) R [I] = min (R [I], y ); for (I I = x + 1; I <= m; I ++) L [I] = max (L [I], Y);} int main () {int I, J, K, X, Y, flag = 0; CIN> seed> A> B> C> D; cin> m> N> q; for (I = 1; I <= m; I ++) R [I] = N; for (I = 1; I <= m * n; I ++) T [I] = I, swap (T [I], t [rand () % I + 1]); for (I = 1; I <= Q; I ++) {scanf ("% d", & X, & Y); swap (T [X], T [y]);} k = 0; for (I = 1; I <= m; I ++) for (j = 1; j <= N; j ++) map [I] [J] = T [++ K]; for (I = 1; I <= m; I ++) for (j = 1; j <= N; j ++) T [map [I] [J] = I <16 | j; for (I = 1; I <= m * N; I ++) {x = T [I]> 16; y = T [I] & 65535; if (y = L [x] & Y <= R [x]) {If (FLAG) putchar (''); flag = 1; printf ("% d", map [x] [Y]); kill (x, y );}}}
Bzoj 3671 noi2014 random number generator greedy + violent