QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#446380 | #4788. Gravity | C1942huangjiaxu | ML | 31ms | 215288kb | C++14 | 1.5kb | 2024-06-17 09:22:05 | 2024-06-17 09:22:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=4e6+5;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1},dw[N],dwp[N];
int n,m,id[N],ci,d[N];
bool vis[N];
vector<pair<int,int> >e[N];
char *s[N],t[N];
vector<pair<int,int> >nd[N];
priority_queue<pair<int,int> >q;
#define h(x,y) ((x)*m-m+(y))
void dfs(int x,int y){
id[h(x,y)]=ci;
nd[ci].emplace_back(x,y);
for(int i=0;i<4;++i){
int X=x+dx[i],Y=y+dy[i];
if(X<1||X>n||Y<1||Y>m||s[X][Y]=='.')continue;
if(!id[h(X,Y)])dfs(X,Y);
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
s[i]=new char[m+3];
scanf("%s",s[i]+1);
}
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(s[i][j]=='#'&&!id[h(i,j)])++ci,dfs(i,j);
for(int i=1;i<=m;++i)dwp[h(n,i)]=n+1;
for(int i=n-1;i;--i){
for(int j=1;j<=m;++j){
if(s[i+1][j]=='#')dw[h(i,j)]=id[h(i+1,j)],dwp[h(i,j)]=i+1;
else dw[h(i,j)]=dw[h(i+1,j)],dwp[h(i,j)]=dwp[h(i+1,j)];
}
}
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(id[h(i,j)]&&dw[h(i,j)]!=id[h(i,j)]){
int u=id[h(i,j)],v=dw[h(i,j)];
e[v].emplace_back(u,dwp[h(i,j)]-i-1);
}
memset(d,0x3f,sizeof(d));
d[0]=0,q.emplace(0,0);
while(!q.empty()){
int u=q.top().second;
q.pop();
if(vis[u])continue;
vis[u]=true;
for(auto [x,v]:e[u])if(d[x]>d[u]+v){
d[x]=d[u]+v;
q.emplace(-d[x],x);
}
}
for(int i=1;i<=n*m;++i)t[i]='.';
for(int i=1;i<=ci;++i)for(auto [x,y]:nd[i])t[h(x+d[i],y)]='#';
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j)putchar(t[h(i,j)]);
putchar(10);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 31ms
memory: 215288kb
input:
10 10 .......... ..######.. ..#....#.. ..#.#..#.. ..#..#.#.. ..#....#.. ..######.. .......... ..#....#.. .......#..
output:
.......... .......... ..######.. ..#....#.. ..#....#.. ..#....#.. ..#.##.#.. ..######.. .......#.. ..#....#..
result:
ok 10 lines
Test #2:
score: -100
Memory Limit Exceeded
input:
1583 1799 #..###..##..#.####.##.##.###..#.....##..#.#.#.#......#.....##..#.##...#.#....#..##.###...#...##.###.#.....#.##.###...#..##.#...###..#.###.#...###..#.......#...#....#.#..#...##........#.#..#..##.....###...#..#.####..####...#..##......#........#..#.##.##..#..#....##.##.##..#..##.....###....#...
output:
...............................................................................................................................................................................................................................................................................................................