QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#446747 | #4788. Gravity | zhaohaikun | ML | 0ms | 0kb | C++20 | 2.5kb | 2024-06-17 15:40:51 | 2024-06-17 15:40:51 |
answer
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\033[32m[" << __LINE__ << "]\033[0m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T& x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = - f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x *= f;
}
const int N = 4e6 + 10;
// vector <char> v[N];
string st[N], ans[N];
int n, m, fa[N], f[N];
bool vis[N];
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
int id(int x, int y) {
return (x - 1) * m + y;
}
// set <pair <int, int>> s[N];
int lst[N];
// vector <char> ans[]
vector <pair <int, int>> v[N];
signed main() {
read(n), read(m);
F(i, 1, n) {
cin >> st[i];
st[i] = ' ' + st[i];
ans[i] = st[i];
F(j, 1, m) {
ans[i][j] = '.';
fa[id(i, j)] = id(i, j);
if (st[i][j] == '#') {
if (j > 1 && st[i][j - 1] == '#') {
fa[find(id(i, j))] = find(id(i, j - 1));
}
if (i > 1 && st[i - 1][j] == '#') {
fa[find(id(i, j))] = find(id(i - 1, j));
}
}
}
}
// ms(t, 0x3f);
// F(i, 1, m) t[i] =
F(j, 1, m) {
int lst = 0;
DF(i, n, 1) {
if (st[i][j] == '#') {
if (!lst) {
v[0].emplace_back(find(id(i, j)), n - i);
// chkmin(t[find(id(i, j))], n - i);
} else if (find(id(i, j)) != find(id(lst, j))) {
v[find(id(lst, j))].emplace_back(find(id(i, j)), lst - i - 1);
}
lst = i;
}
}
}
ms(f, 0x3f);
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;
q.emplace(f[0] = 0, 0);
while (q.size()) {
int x = q.top().second; q.pop();
if (vis[x]) continue;
vis[x] = true;
for (auto [i, j]: v[x])
if (f[x] + j < f[i]) q.emplace(f[i] = f[x] + j, i);
}
// debug << f[find(id(2, 1))] << endl;
F(i, 1, n)
F(j, 1, m)
if (st[i][j] == '#') {
ans[i + f[find(id(i, j))]][j] = '#';
// debug << i + f[find(id(i, j))] << " " << j << endl;
}
F(i, 1, n) {
F(j, 1, m)
cout << ans[i][j];
cout << '\n';
}
return 0;
}
/* why?
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Memory Limit Exceeded
input:
10 10 .......... ..######.. ..#....#.. ..#.#..#.. ..#..#.#.. ..#....#.. ..######.. .......... ..#....#.. .......#..
output:
.......... .......... ..######.. ..#....#.. ..#....#.. ..#....#.. ..#.##.#.. ..######.. .......#.. ..#....#..