Solution
题干教我们用
n∗m+q
的时间求出一个矩阵
要求我们在矩阵上走一条路径 使得路径排序后字典序最小
那么 就很显然了
采用贪心的思想 依次考虑每个数字
每个数字需要一个“来路”和“去路”
最后记录一下可行的答案即可
Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define oo 2139062143
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-x))
#define abs(x) (((x)>=0)?(x):(-(x)))
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
#define divi(x,y) (((x)%(y))?((x)/(y)):(((x)/(y)))-1)
#define fo(i,x,y) for (int i = (x);i <= (y);++ i)
#define fd(i,x,y) for (int i = (x);i >= (y);-- i)
using namespace std;
typedef double db;
typedef long long ll;
const int N = 5050,M = 5050,NM = N * M;
ll a,b,c,d;
int n,m,q,nm,tot;
int t[NM],x[NM],upp[N],low[N],ans[N * 2];
void ins(int a,int b)
{
fo(i,1,n)
if (i < a) upp[i] = min(b,upp[i]); else
if (i > a) low[i] = max(b,low[i]);
}
int main()
{
scanf("%d%lld%lld%lld%lld", &x[0], &a, &b, &c, &d);
scanf("%d%d%d", &n, &m, &q); nm = n * m;
memset(upp,127,sizeof upp);
fo(i,1,nm) x[i] = (x[i - 1] * (a * x[i - 1] + b) + c) % d,t[i] = i;
fo(i,1,nm) swap(t[i],t[(x[i] % i) + 1]);
fo(i,1,q)
{
int x,y;
scanf("%d%d", &x, &y);
swap(t[x],t[y]);
}
fo(i,1,nm) x[t[i]] = i;
fo(i,1,nm)
{
int a,b;
if (x[i] % m) a = x[i] / m + 1;
else a = x[i] / m;
b = x[i] % m;
if (!b) b += m;
if (b <= upp[a] && b >= low[a])
{
ins(a,b);
ans[++ tot] = i;
if (tot == n + m - 1) break;
}
}
fo(i,1,tot) printf("%d ", ans[i]);
}