QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#446315 | #4788. Gravity | Kevin5307 | RE | 12ms | 81760kb | C++23 | 2.4kb | 2024-06-17 07:46:33 | 2024-06-17 07:46:34 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;
using i128=__int128;
void die(string S){puts(S.c_str());exit(0);}
int n,m;
// char grid[2005][2005],ans[2005][2005];
string grid[1000005],ans[1000005];
vector<int> ind[1000005];
int fa[4000005];
inline int anc(int x)
{
while(fa[x]!=x) x=fa[x]=fa[fa[x]];
return x;
}
inline void join(int u,int v)
{
fa[anc(u)]=anc(v);
}
vector<pii> G[4000005];
int dist[4000005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>grid[i];
for(int i=0;i<n;i++)
ind[i].resize(m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
ind[i][j]=(i*m+j);
for(int i=0;i<n*m;i++)
fa[i]=i;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(grid[i][j]=='#')
{
if(grid[i+1][j]=='#') join(ind[i][j],ind[i+1][j]);
if(grid[i][j+1]=='#') join(ind[i][j],ind[i][j+1]);
}
for(int j=0;j<m;j++)
{
int lst=-1;
for(int i=0;i<n;i++)
if(grid[i][j]=='#')
{
if(~lst)
G[anc(ind[i][j])].pb(anc(ind[lst][j]),i-lst-1);
lst=i;
}
}
memset(dist,0x3f,sizeof(dist));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(grid[i][j]=='#')
dist[anc(ind[i][j])]=min(dist[anc(ind[i][j])],n-i-1);
priority_queue<pii,vector<pii>,greater<pii>> pq;
for(int i=0;i<n*m;i++)
if(fa[i]==i)
pq.emplace(dist[i],i);
while(!pq.empty())
{
int d=pq.top().first;
int u=pq.top().second;
pq.pop();
if(dist[u]!=d) continue;
for(auto [v,w]:G[u])
if(dist[u]+w<dist[v])
{
dist[v]=dist[u]+w;
pq.emplace(dist[v],v);
}
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
ans[i][j]='.';
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(grid[i][j]=='#')
ans[i+dist[anc(ind[i][j])]][j]='#';
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
putchar(ans[i][j]);
putchar(10);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 12ms
memory: 81760kb
input:
10 10 .......... ..######.. ..#....#.. ..#.#..#.. ..#..#.#.. ..#....#.. ..######.. .......... ..#....#.. .......#..
output:
.......... .......... ..######.. ..#....#.. ..#....#.. ..#....#.. ..#.##.#.. ..######.. .......#.. ..#....#..
result:
ok 10 lines
Test #2:
score: -100
Runtime Error
input:
1583 1799 #..###..##..#.####.##.##.###..#.....##..#.#.#.#......#.....##..#.##...#.#....#..##.###...#...##.###.#.....#.##.###...#..##.#...###..#.###.#...###..#.......#...#....#.#..#...##........#.#..#..##.....###...#..#.####..####...#..##......#........#..#.##.##..#..#....##.##.##..#..##.....###....#...