BZOJ2054: 疯狂的馒头(并查集)
程序员文章站
2022-04-14 20:45:54
题意 "题目链接" Sol 直接倒着并查集一波就行了 cpp include // define int long long using namespace std; const int MAXN = 1e6 + 10; inline int read() { char c = getchar(); ......
题意
sol
直接倒着并查集一波就行了
#include<bits/stdc++.h> // #define int long long using namespace std; const int maxn = 1e6 + 10; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int n, m, p, q, ans[maxn], fa[maxn]; int find(int x) { return fa[x] == x ? fa[x] : fa[x] = find(fa[x]); } main() { // freopen("a.in", "r", stdin); n = read(); m = read(); p = read(); q = read(); for(int i = 1; i <= n + 1; i++) fa[i] = i; for(int i = m; i >= 1; i--) { int l = (i * p + q) % n + 1, r = (i * q + p) % n + 1; if(l > r) swap(l, r); for(int j = find(l); j <= r; j = (fa[j] = find(j + 1))) ans[j] = i; } for(int i = 1; i <= n; i++) printf("%d\n", ans[i]); return 0; }
上一篇: 初识HTML_表单
下一篇: Shell 编程 循环语句