[BZOJ3671][Noi2014]随机数生成器(模拟+贪心)

Address

https://www.luogu.org/problemnew/show/P2354
https://www.lydsy.com/JudgeOnline/problem.php?id=3671
https://loj.ac/problem/2248
http://uoj.ac/problem/6

Solution

首先模拟一下,得出棋盘上每个格子上的数。
而我们要求一条 ( 1 , 1 ) ( N , M ) 的最短路径,使得路径上的数排序之后字典序最小。
字典序显然可以贪心:
按格子上的数从小到大考虑每个格子,到了格子 ( x , y )
如果对于之前加入路径的任意一个格子 ( u , v ) 都满足 u x , v y u x , v y ,那么易得存在一种方案,只往右和往下走且能经过所有已经加入路径的格子。把 ( x , y ) 加入路径,否则考虑下一个格子。
具体地,我们对于每个 1 i N ,维护第 i 行第一个和最后一个已经加入路径的格子的纵坐标(分别为 m i n d [ i ] m a x d [ i ] )(如果没有则为 0 ),
插入格子 ( x , y ) 时,只要找出所有满足 z < x 且第 z 行有已经加入路径的格子的行中最大的一个 z ,如果 m a x d [ z ] > y 则不合法(如果没有这样的 z 则忽略这一步),然后找出 z > x 且第 z 行有已经加入路径的格子的行中最小的一个 z ,如果 m i n d [ z ] < y 则不合法(如果没有这样的 z 则忽略这一步)。
这样复杂度是三次方的。
我们记录 p r e n x t 分别表示某一行往上和往下(不包括本身)第一个存在已经加入路径的格子的行。
每次找到一个合法的格子时暴力修改 p r e n x t
这样复杂度就是 O ( N M + N 2 ) 的。

Code

注意卡空间

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, a, b) for (i = a; i <= b; i++)
#define Rof(i, a, b) for (i = a; i >= b; i--)
using namespace std;

inline int read()
{
    int res = 0; bool bo = 0; char c;
    while (((c = getchar()) < '0' || c > '9') && c != '-');
    if (c == '-') bo = 1; else res = c - 48;
    while ((c = getchar()) >= '0' && c <= '9')
        res = (res << 3) + (res << 1) + (c - 48);
    return bo ? ~res + 1 : res;
}

const int N = 5005, M = 5e4 + 5, L = N * N;

int X0, a, b, c, d, n, m, q, _u[M], _v[M], T[L], A[L], l, mind[N],
maxd[N], ToT, pre[N], nxt[N];

inline void Swap(int &a, int &b) {a ^= b; b ^= a; a ^= b;}
inline int Min(const int &a, const int &b) {return a < b ? a : b;}
inline int Max(const int &a, const int &b) {return a > b ? a : b;}

int main()
{
    int i, j;
    X0 = read(); a = read(); b = read(); c = read(); d = read();
    n = read(); m = read(); q = read();
    For (i, 1, q)
        _u[i] = read(), _v[i] = read();
    l = n * m;
    For (i, 1, l) T[i] = i;
    For (i, 1, l)
    {
        X0 = (1ll * (1ll * a * X0 + b) * X0 + c) % d;
        int rev = X0 < i ? X0 + 1 : X0 % i + 1;
        if (i != rev) Swap(T[i], T[rev]);
    }
    For (i, 1, q) if (_u[i] != _v[i])
        Swap(T[_u[i]], T[_v[i]]);
    For (i, 1, l) A[T[i]] = i;
    For (i, 1, l)
    {
        int _r = (A[i] - 1) / m + 1, _c = A[i] - (_r - 1) * m;
        int nmin = mind[_r] ? Min(mind[_r], _c) : _c,
            nmax = maxd[_r] ? Max(maxd[_r], _c) : _c;
        if ((pre[_r] && maxd[pre[_r]] && maxd[pre[_r]] > nmin)
            || (nxt[_r] && mind[nxt[_r]] && mind[nxt[_r]] < nmax))
                continue;
        ToT++;
        printf("%d ", i);
        if (!mind[_r])
        {
            Rof (j, _r - 1, 1)
            {
                nxt[j] = _r;
                if (mind[j]) break;
            }
            For (j, _r + 1, n)
            {
                pre[j] = _r;
                if (mind[j]) break;
            }
        }
        mind[_r] = nmin; maxd[_r] = nmax;
        if (ToT == n + m - 1) break;
    }
    cout << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xyz32768/article/details/82527932