From c1e137487c51f12b560209b147393eae1accb157 Mon Sep 17 00:00:00 2001 From: Macesuted Date: Thu, 7 Nov 2024 19:16:34 +0800 Subject: [PATCH] QOJ: The 2nd Universal Cup. Stage 10: Harbin --- QOJ/7747.cpp | 40 ++++++++++++++++++ QOJ/7751.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ QOJ/7752.cpp | 83 +++++++++++++++++++++++++++++++++++++ QOJ/7757.cpp | 46 +++++++++++++++++++++ 4 files changed, 283 insertions(+) create mode 100644 QOJ/7747.cpp create mode 100644 QOJ/7751.cpp create mode 100644 QOJ/7752.cpp create mode 100644 QOJ/7757.cpp diff --git a/QOJ/7747.cpp b/QOJ/7747.cpp new file mode 100644 index 0000000..eb47f29 --- /dev/null +++ b/QOJ/7747.cpp @@ -0,0 +1,40 @@ +/** + * @file 7747.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-11-07 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define endl '\n' + +void solve(void) { + int n; + cin >> n; + bool rest = false; + int v = 0; + for (int i = 1, x; i <= n; i++) { + cin >> x; + if (v % 2) v--, rest = true; + v = v / 2 + x; + if (v == 0 && !rest) + cout << '0'; + else + cout << "-+"[v >= 0]; + } + cout << endl; + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr); + + int _ = 1; + while (_--) solve(); + + return 0; +} \ No newline at end of file diff --git a/QOJ/7751.cpp b/QOJ/7751.cpp new file mode 100644 index 0000000..de10baa --- /dev/null +++ b/QOJ/7751.cpp @@ -0,0 +1,114 @@ +/** + * @file 7751.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-11-07 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define endl '\n' + +#define maxn 35 +#define maxv 810005 + +typedef pair pii; + +const int way[4][2] = {{0, -1}, {0, +1}, {-1, 0}, {+1, 0}}; + +string a[maxn]; +pii nxt[maxv]; +int dis[maxv]; +bool vis[maxn][maxn]; +vector graph[maxv], rgraph[maxv]; + +int _(pii x, pii y) { return (x.first - 1) * 27000 + (x.second - 1) * 900 + (y.first - 1) * 30 + (y.second - 1); } +pair $(int v) { return {{v / 27000 + 1, v / 900 % 30 + 1}, {v / 30 % 30 + 1, v % 30 + 1}}; } + +void solve(void) { + int n, m; + cin >> n >> m; + + a[0] = a[n + 1] = string(m + 2, '0'); + for (int i = 1; i <= n; i++) cin >> a[i], a[i] = '0' + a[i] + '0'; + + auto addEdge = [&](int p, int q, int t) { return graph[p].emplace_back(q, t), rgraph[q].emplace_back(p, t); }; + + for (int ax = 1; ax <= n; ax++) + for (int ay = 1; ay <= m; ay++) + for (int bx = 1; bx <= n; bx++) + for (int by = 1; by <= m; by++) + if (a[ax][ay] == '1' && a[bx][by] == '1') + for (int t = 0; t < 4; t++) { + int tax = ax + way[t][0], tay = ay + way[t][1]; + if (a[tax][tay] == '0') tax = ax, tay = ay; + int tbx = bx - way[t][0], tby = by - way[t][1]; + if (a[tbx][tby] == '1') addEdge(_({ax, ay}, {bx, by}), _({tax, tay}, {tbx, tby}), t); + if (a[bx + way[t][0]][by + way[t][1]] == '0') + addEdge(_({ax, ay}, {bx, by}), _({tax, tay}, {bx, by}), t); + } + + for (int i = 0; i < maxv; i++) nxt[i] = {-1, -1}, dis[i] = -1; + queue que; + for (int x = 1; x <= n; x++) + for (int y = 1; y <= m; y++) + if (a[x][y] == '1') nxt[_({x, y}, {x, y})] = {0, 4}, dis[_({x, y}, {x, y})] = 0, que.push(_({x, y}, {x, y})); + while (!que.empty()) { + int p = que.front(); + que.pop(); + for (auto [q, t] : rgraph[p]) + if (dis[q] == -1) nxt[q] = {p, t}, dis[q] = dis[p] + 1, que.push(q); + } + + int sx, sy, tx, ty; + cin >> sx >> sy >> tx >> ty; + if (dis[_({sx, sy}, {tx, ty})] == -1) return cout << -1 << endl, void(); + mt19937 rnd(114514); + while (clock() * 1000. / CLOCKS_PER_SEC < 900) { + int rest = 0; + for (int i = 1; i <= n; i++) + for (int j = 1; j <= m; j++) + if (a[i][j] == '1') vis[i][j] = true, rest++; + + int p = _({sx, sy}, {tx, ty}); + string ans; + rest -= vis[sx][sy], vis[sx][sy] = false; + rest -= vis[tx][ty], vis[tx][ty] = false; + + while (rest) { + bool mov = false; + shuffle(graph[p].begin(), graph[p].end(), rnd); + for (auto [q, t] : graph[p]) + if (dis[q] != -1 && (int)ans.size() + 1 + dis[q] <= int(1e6)) { + p = q, mov = true, ans.push_back("LRUD"[t]); + auto [u, v] = $(p); + rest -= vis[u.first][u.second], vis[u.first][u.second] = false; + rest -= vis[v.first][v.second], vis[v.first][v.second] = false; + break; + } + if (!mov) break; + } + if (rest) continue; + while (dis[p]) ans.push_back("LRUD"[nxt[p].second]), p = nxt[p].first; + string nans = ans; + reverse(ans.begin(), ans.end()); + nans.append(ans.begin(), ans.end()); + cout << nans << endl; + + return; + } + cout << -1 << endl; + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr); + + int _ = 1; + while (_--) solve(); + + return 0; +} \ No newline at end of file diff --git a/QOJ/7752.cpp b/QOJ/7752.cpp new file mode 100644 index 0000000..718a3e8 --- /dev/null +++ b/QOJ/7752.cpp @@ -0,0 +1,83 @@ +/** + * @file 7752.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-11-07 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define endl '\n' + +#define maxn 2000005 + +typedef pair pii; +typedef tuple tiii; + +class UnionSet { + private: + int fa[maxn]; + + int getfa(int p) { return p == fa[p] ? p : fa[p] = getfa(fa[p]); } + + public: + UnionSet(void) { + for (int i = 0; i < maxn; i++) fa[i] = i; + } + + bool merge(int x, int y) { + x = getfa(x), y = getfa(y); + if (x == y) return false; + return fa[x] = y, true; + } +} US; + +int ucnt = 0; +map> S; +int cross(tiii x, tiii y) { return max(0, min(get<1>(x), get<1>(y)) - max(get<0>(x), get<0>(y)) + 1); } + +bool solve(void) { + int n, m, k; + cin >> n >> m >> k; + + for (int i = 1, xl, xr, y; i <= k; i++) cin >> xl >> xr >> y, S[y].emplace_back(xl, xr); + + if (n == 1 || m == 1) return true; + + vector lst; + for (int i = 1; i <= m; i++) { + vector cur; + if (!S.count(i)) + cur.emplace_back(1, n, ++ucnt); + else { + vector &vec = S[i]; + vec.emplace_back(0, 0), vec.emplace_back(n + 1, n + 1); + sort(vec.begin(), vec.end()); + for (int j = 0; j + 1 < (int)vec.size(); j++) + if (vec[j].second + 1 < vec[j + 1].first) cur.emplace_back(vec[j].second + 1, vec[j + 1].first - 1, ++ucnt); + } + for (auto x = lst.begin(), y = cur.begin(); x != lst.end() && y != cur.end(); y++) { + while (x != lst.end() && get<1>(*x) < get<0>(*y)) x++; + while (x != lst.end() && get<1>(*x) <= get<1>(*y)) { + if (cross(*x, *y) > 1 || !US.merge(get<2>(*x), get<2>(*y))) return false; + x++; + } + if (x != lst.end() && cross(*x, *y)) + if (cross(*x, *y) > 1 || !US.merge(get<2>(*x), get<2>(*y))) return false; + } + lst = cur; + } + return true; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr); + + int _ = 1; + while (_--) cout << (solve() ? "YES" : "NO") << endl; + + return 0; +} \ No newline at end of file diff --git a/QOJ/7757.cpp b/QOJ/7757.cpp new file mode 100644 index 0000000..c263a83 --- /dev/null +++ b/QOJ/7757.cpp @@ -0,0 +1,46 @@ +/** + * @file 7757.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-11-07 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define endl '\n' + +void solve(void) { + int n; + cin >> n; + + vector a(n + 1), pos(n + 1); + + for (int i = 1; i <= n; i++) cin >> a[i]; + for (int i = 1, x; i <= n; i++) cin >> x, pos[x] = i; + for (int i = 1; i <= n; i++) a[i] = pos[a[i]]; + + string ans; + for (int i = 1; i < n; i++) { + for (int j = 1; j < n; j++) + if (a[j] > a[j + 1]) + ans.push_back('2'), swap(a[j], a[j + 1]); + else + ans.push_back('1'); + ans.push_back('1'); + } + cout << ans << endl; + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr); + + int _ = 1; + cin >> _; + while (_--) solve(); + + return 0; +} \ No newline at end of file