1. 程式人生 > >BZOJ3671 [Noi2014]隨機數生成器 【貪心】

BZOJ3671 [Noi2014]隨機數生成器 【貪心】

n) 隨機數 lag 直接 memset ++ oid putchar esp

題目鏈接

BZOJ3671

題解

模擬題意生成矩陣貪心從小選擇即可
每選擇一個,就標記其左下右上矩陣
由於每次都是標記一個到邊界的矩陣,所以一旦遇到標記過就直接退出即可,可以保證復雜度
還有就是空間和時間有點卡

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++) #define mp(a,b) make_pair<int,int>(a,b) #define cls(s) memset(s,0,sizeof(s)) #define cp pair<int,int> #define LL long long int #define res register using namespace std; const int maxn = 5005,maxm = 25000005,INF = 1000000000; inline int read(){ int out = 0
,flag = 1; char c = getchar(); while (c < 48 || c > 57){if (c == ‘-‘) flag = -1; c = getchar();} while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();} return out * flag; } inline void write(int x){ if (x / 10) write(x / 10); putchar(x % 10
+ ‘0‘); } LL x,a,b,c,d; int n,m,K,Q,A[maxm],ans[maxn << 1],pos[maxm]; bool vis[maxn][maxn]; int main(){ x = read(); a = read(); b = read(); c = read(); d = read(); n = read(); m = read(); Q = read(); K = n * m; for (res int i = 1; i <= K; i++){ *(A + i) = i; x = ((a * x + b) * x + c) % d; swap(*(A + i),*(A + x % i + 1)); } while (Q--) swap(*(A + read()),*(A + read())); for (res int i = 1; i <= K; i++) *(pos + *(A + i)) = i; res int cnt = 0,E = n + m,x,y; for (res int i = 1; i <= K && cnt < E; i++){ if (*(pos + i) % m == 0){ x = *(pos + i) / m; y = m; } else { x = *(pos + i) / m + 1; y = *(pos + i) % m; } if (!*(*(vis + x) + y)){ ans[++cnt] = i; if (y < m) for (res int j = x - 1; j; j--){ if (*(*(vis + j) + y + 1)) break; for (res int k = y + 1; k <= m; k++){ if (*(*(vis + j) + k)) break; *(*(vis + j) + k) = true; } } if (y > 1) for (res int j = x + 1; j <= n; j++){ if (*(*(vis + j) + y - 1)) break; for (res int k = y - 1; k; k--){ if (*(*(vis + j) + k)) break; *(*(vis + j) + k) = true; } } } } for (res int i = 1; i < E; i++) write(*(ans + i)),putchar(‘ ‘); return 0; }

BZOJ3671 [Noi2014]隨機數生成器 【貪心】