QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#446447#4788. GravityC1942huangjiaxuTL 159ms34084kbC++172.3kb2024-06-17 10:48:032024-06-17 10:48:04

Judging History

你现在查看的是最新测评结果

  • [2024-06-17 10:48:04]
  • 评测
  • 测评结果:TL
  • 用时:159ms
  • 内存:34084kb
  • [2024-06-17 10:48:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2003,M=2000001;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int n,m,id[N][N],ci;
short d[M],w[M],dw[N][N];
bool vis[M];
int hd[M],to[M],nx[M],num;
void add(int x,int y,short z){
	nx[++num]=hd[x],hd[x]=num,to[num]=y,w[num]=z;
}
char s[N][N];
struct heap{
int p[M],rp[M],ct;
short va[M];
bool empty(){
	return !ct;
}
int top(){
	return p[1];
}
void push(int x,short v){
	if(!rp[x])rp[x]=++ct,p[ct]=x;
	int o=rp[x];
	va[rp[x]]=v;
	while(o>1){
		if(va[o]>=va[o>>1])break;
		swap(rp[p[o]],rp[p[o>>1]]);
		swap(p[o],p[o>>1]);
		swap(va[o],va[o>>1]);
		o>>=1;
	}
}
void pop(){
	swap(rp[p[1]],rp[p[ct]]);
	swap(p[1],p[ct]),swap(va[1],va[ct]);
	rp[p[ct]]=0,--ct;
	int o=1;
	while(o<<1<=ct){
		if((o<<1|1)>ct||va[o<<1|1]>=va[o<<1]){
			if(va[o]<=va[o<<1])break;
			o<<=1;
			swap(rp[p[o]],rp[p[o>>1]]);
			swap(p[o],p[o>>1]);
			swap(va[o],va[o>>1]);
		}else{
			if(va[o]<=va[o<<1|1])break;
			o<<=1,o|=1;
			swap(rp[p[o]],rp[p[o>>1]]);
			swap(p[o],p[o>>1]);
			swap(va[o],va[o>>1]);
		}
	}
}
}q;
void bfs(short sx,short sy){
	queue<pair<short,short>>q;
	q.emplace(sx,sy);
	while(!q.empty()){
		auto [x,y]=q.front();
		q.pop();
		id[x][y]=ci;
		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[X][Y])q.emplace(X,Y);
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)scanf("%s",s[i]+1);
	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(s[i][j]=='#'&&!id[i][j])++ci,bfs(i,j);
	for(int i=1;i<=m;++i)dw[n][i]=n+1;
	for(int i=n-1;i;--i)
		for(int j=1;j<=m;++j){
			if(s[i+1][j]=='#')dw[i][j]=i+1;
			else dw[i][j]=dw[i+1][j];
		}
	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)
		if(id[i][j]&&id[dw[i][j]][j]!=id[i][j])add(id[dw[i][j]][j],id[i][j],dw[i][j]-i-1);
	for(int i=1;i<=ci;++i)d[i]=1e4;
	d[0]=0,q.push(0,0);
	while(!q.empty()){
		int u=q.top();
		q.pop();
		if(vis[u])continue;
		vis[u]=true;
		for(int i=hd[u],v;v=to[i],i;i=nx[i])if(d[v]>d[u]+w[i]){
			d[v]=d[u]+w[i];
			q.push(v,d[v]);
		}
	}
	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)s[i][j]='.';
	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if(id[i][j])s[i+d[id[i][j]]][j]='#';
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j)putchar(s[i][j]);
		putchar(10);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3656kb

input:

10 10
..........
..######..
..#....#..
..#.#..#..
..#..#.#..
..#....#..
..######..
..........
..#....#..
.......#..


output:

..........
..........
..######..
..#....#..
..#....#..
..#....#..
..#.##.#..
..######..
.......#..
..#....#..

result:

ok 10 lines

Test #2:

score: 0
Accepted
time: 159ms
memory: 34084kb

input:

1583 1799
#..###..##..#.####.##.##.###..#.....##..#.#.#.#......#.....##..#.##...#.#....#..##.###...#...##.###.#.....#.##.###...#..##.#...###..#.###.#...###..#.......#...#....#.#..#...##........#.#..#..##.....###...#..#.####..####...#..##......#........#..#.##.##..#..#....##.##.##..#..##.....###....#...

output:

...............................................................................................................................................................................................................................................................................................................

result:

ok 1583 lines

Test #3:

score: 0
Accepted
time: 19ms
memory: 12624kb

input:

592 750
.......#..#.#......#.............#...............###..#..#.........#.#.......##.............#.#.#................#..#...#...#......#...#.............#..##..#.#..#..........#..##.#.#..#..#.#...#....#......####.........#..#......#...........#......#............#.###.......##.#..#.#.#...#.#.##....

output:

...............................................................................................................................................................................................................................................................................................................

result:

ok 592 lines

Test #4:

score: -100
Time Limit Exceeded

input:

1768 1394
###.###.#######.###.#####.####.##########################.########.###################################.#########################################.#########################.####################.##############.###########################.#######################.################.####.#.#######...

output:


result: