一道 dls 调整法练习题
zmx_wzx_JY · · 个人记录
构造一个
目前的效率大概是
大体思路:
1.首先满足偶数不相邻的限制:把棋盘黑白染色,偶数放进其中一种颜色。之后调整的时候只交换同奇偶性的数对。
2.随机调整,估价为矛盾的对数。如果交换两个估价更小就换,如果估价相同随机换(实测上相等时只要有比较高的概率换就比较快,为了省随机数速度就直接强制换了)。
直接这么写大概就可以跑
发现在
于是再维护一个
大概可以跑几百了,但是末段速度依然很慢,因为我们大量随机到不需要交换的对。
于是搞各栈维护当前
此时大概可以 10s 内出 1000 的解,在优化一下算估价的速度(如预处理 gcd)这些的就能到 2s 左右。
最终代码:
#include<bits/stdc++.h>
#define ll long long
#define rep(i, x, y) for(int i = (x), stOyny = (y); i <= stOyny; ++i)
#define irep(i, x, y) for(int i = (x), stOyny = (y); i >= stOyny; --i)
#define pb emplace_back
#define pii pair<int, int>
#define vint vector<int>
#define let const auto
#define fi first
#define se second
#define il inline
#define ci const int
#define all(S) S.begin(), S.end()
int read() {
int res = 0, flag = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') flag = -1; c = getchar(); }
while(c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar();
return res * flag;
}
using namespace std;
template<typename T>
il void tmax(T &x, const T y) { if(x < y) x = y; }
template<typename T>
il void tmin(T &x, const T y) { if(x > y) x = y; }
using namespace std;
template<typename T>
il void tmin(T &x, const T& y) { if(x > y) x = y; }
template<typename T>
il void tmax(T &x, const T& y) { if(x < y) x = y; }
const int N = 1005, M = 10000;
int n, m, mp[N][N], K, a[N][N];
bitset<M + 5> bc[M + 5];
bool chk(int x, int y) {
if(x <= M && y <= M) return bc[x][y];
if(x < y) swap(x, y);
if(!y) return x != 1;
return chk(y, x % y);
}
int calc(int x, int y) {
int res = 0;
res += x != 1 && chk(a[x][y], a[x - 1][y]);
res += x != n && chk(a[x][y], a[x + 1][y]);
res += y != 1 && chk(a[x][y], a[x][y - 1]);
res += y != m && chk(a[x][y], a[x][y + 1]);
return res;
}
int derta(int ax, int ay, int bx, int by) {
int res = -calc(ax, ay) - calc(bx, by);
swap(a[ax][ay], a[bx][by]);
res += calc(ax, ay) + calc(bx, by);
swap(a[ax][ay], a[bx][by]);
return res;
}
int cnt[N][N];
void change(int x, int y, int op) {
if(x != 1 && chk(a[x][y], a[x - 1][y])) cnt[x][y] += op, cnt[x - 1][y] += op;
if(x != n && chk(a[x][y], a[x + 1][y])) cnt[x][y] += op, cnt[x + 1][y] += op;
if(y != 1 && chk(a[x][y], a[x][y - 1])) cnt[x][y] += op, cnt[x][y - 1] += op;
if(y != m && chk(a[x][y], a[x][y + 1])) cnt[x][y] += op, cnt[x][y + 1] += op;
}
mt19937 rnd(time(0));
signed main() {
// freopen("1.in", "r", stdin);
n = read(), m = read(), K = n * m;
auto stt = clock();
rep(x, 0, M) rep(y, 0, x) bc[y][x] = bc[x][y] = !y ? (x != 1) : bc[y][x % y];
cout << (double)(clock() - stt) / CLOCKS_PER_SEC << '\n';
int np = 2;
rep(i, 1, n) rep(j, 1, m) if((i + j) % 2 == 0 && np <= K) a[i][j] = np, np += 2;
np = 1;
rep(i, 1, n) rep(j, 1, m) if(!a[i][j] && np <= K) a[i][j] = np, np += 2;
int ans = 0;
rep(i, 1, n) rep(j, 1, m) ans += cnt[i][j] = calc(i, j);
ans /= 2;
auto rd = [&](int x) { return rnd() % x + 1; };
int ct = 0;
auto st = clock();
static vector<pii> d;
static bool inq[N][N];
auto ins = [&](int x, int y) {
if(cnt[x][y] && !inq[x][y]) inq[x][y] = true, d.pb(x, y);
};
int B = 100000;
while(ans) {
int bx = rd(n), by = rd(m);
int ax, ay;
if(ans > B) ax = rd(n), ay = rd(m);
else {
if(!d.size() || (int)d.size() > max(ans * 12, 4000)) {
memset(inq, 0, sizeof(inq));
d.clear();
rep(i, 1, n) rep(j, 1, m) if(cnt[i][j]) d.pb(i, j), inq[i][j] = true;
}
while(1) {
let tt = d[rnd() % d.size()];
ax = tt.fi, ay = tt.se;
if(cnt[ax][ay]) break;
}
}
if((!cnt[ax][ay] && !cnt[bx][by]) || (ax == bx && ay == by) || (a[ax][ay] & 1) != (a[bx][by] & 1)) continue;
int t = derta(ax, ay, bx, by);
if(t <= 0) {
change(ax, ay, -1), change(bx, by, -1);
swap(a[ax][ay], a[bx][by]), ans += t;
change(ax, ay, 1), change(bx, by, 1);
auto expand = [&](int x, int y) {
if(x != 1) ins(x - 1, y);
if(x != n) ins(x + 1, y);
if(y != 1) ins(x, y - 1);
if(y != m) ins(x, y + 1);
};
if(ans <= B) expand(ax, ay), expand(bx, by);
}
++ct;
}
printf("%.9lf %d\n", (double)(clock() - st) / CLOCKS_PER_SEC, ct);
rep(i, 1, n) rep(j, 1, m - 1) assert(__gcd(a[i][j], a[i][j + 1]) == 1);
rep(i, 1, n - 1) rep(j, 1, m) assert(__gcd(a[i][j], a[i + 1][j]) == 1);
return 0;
}
upd:好像有很小概率在最后卡死,可能是因为在最后即使邻居 cnt = 0 也要加进队列?反正应该最后微调一下就好了,或者应该微扰一下也可以,不想管了。