bzoj3671 [Noi2014]随机数生成器

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3671

【题解】

贪心从1...n*m取,开两个5000*5000的数组就够了,可以重复利用,坐标可以压到一个int里。

每次暴力标记不能访问的,标到已经有标记的就不用标了因为后面的肯定前面已经标记过了。

均摊复杂度就对了。复杂度$O(nm)$。

这破题还卡PE..

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e3 + 1;
const int mod = 1e9+7;

int mp[M][M];
int a[M * M], xc, A, B, C, D, n, m, T;
inline int gnext(int x) {
    return (1ll * A * x % D * x % D + 1ll * B * x % D + C) % D;
}

# define id(i, j) (((i)-1) * m + (j))

int main() {
    int Q;
    cin >> xc >> A >> B >> C >> D;
    cin >> n >> m >> Q; T = n*m;
    for (int i=1; i<=T; ++i) a[i] = i;
    for (int i=1; i<=T; ++i) {
        xc = gnext(xc);
        swap(a[i], a[xc % i + 1]);
    }
    while(Q--) {
        scanf("%d%d", &A, &B);
        swap(a[A], a[B]);
    }
    for (int i=1; i<=n; ++i)
        for (int j=1; j<=m; ++j) 
            mp[i][j] = a[id(i, j)];
    for (int i=1; i<=n; ++i)
        for (int j=1; j<=m; ++j)
            a[mp[i][j]] = id(i, j), mp[i][j] = 0;
    int x, y, ok = 1;
    for (int i=1; i<=T; ++i) {
        x = (a[i] - 1) / m + 1;
        y = a[i] - (x-1) * m;
        if(!mp[x][y]) {
            if(ok) printf("%d", i), ok = 0;
            else printf(" %d", i); 
            for (int a=x+1; a<=n; ++a)
                for (int b=y-1; b; --b) {
                    if(mp[a][b]) break; 
                    mp[a][b] = 1; 
                }
            for (int a=x-1; a; --a)
                for (int b=y+1; b<=m; ++b) {
                    if(mp[a][b]) break;
                    mp[a][b] = 1;
                }
        }
    }
    puts("");
    return 0;
}
View Code

 

posted @ 2017-07-01 15:12  Galaxies  阅读(187)  评论(0编辑  收藏  举报