Acwing 第 51 场周赛

贪心,last记录上次被覆盖的最远范围

#include<bits/stdc++.h>
using namespace std;
#define init ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=1e3+5;
typedef pair<int,int> PII;
typedef long long ll;
int n,r;
int res;
int p[N];
int cnt;
int main(){
	init
	cin>>n>>r;
	for(int i=1;i<=n;i++) {
		int t;cin>>t;
		if(t) p[++cnt]=i;
	}
	int last=0;//之前被覆盖的最远
	for(int i=1;i<=cnt;i++){
		if(last>=n) break;
		if(p[i]-r>last){
			res=-1;
			break;
		}
		int j=i;
		while(j+1<=cnt&&p[j+1]-r<=last) j++;
		last=p[j]+r-1;
		i=j;
		res++;
	}
	if(last<n) res=-1;
	cout<<res;
} 

         将所有的空格通过并查集合并,记录大小,对非空格的计算四个方向非重复连通块大小之和

#include<bits/stdc++.h>
using namespace std;
#define init ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=1e3+5;
typedef pair<int,int> PII;
typedef long long ll;
int n,m;
char g[N][N];
bool vis[N][N];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int p[N*N+10];
int cnt[N*N];
int get(int x,int y){
	return (x-1)*m+y;
}
void bfs(int x,int y){
	queue<PII> q;
	q.push({x,y});
	int pos=get(x,y);
	vis[x][y]=true;
	while(!q.empty()){
		PII t=q.front();q.pop();
		int x=t.first,y=t.second;
		for(int i=0;i<4;i++){
			int nx=x+dx[i],ny=y+dy[i];
			if(nx<1||ny<1||nx>n||ny>m) continue;
			if(g[nx][ny]=='*') continue;
			if(vis[nx][ny]) continue;
			vis[nx][ny]=true;
			p[get(nx,ny)]=pos;
			cnt[pos]++;
			q.push({nx,ny});
		}
	}
}
int solve(int x,int y){
	vector<int> vc;
	for(int i=0;i<4;i++){
		int nx=x+dx[i],ny=y+dy[i];
		if(nx<1||ny<1||nx>n||ny>m) continue;
		if(g[nx][ny]=='*') continue;
		vc.push_back(p[get(nx,ny)]);
	}
	sort(vc.begin(),vc.end());
	int n=unique(vc.begin(),vc.end())-vc.begin();
	int res=1;
	for(int i=0;i<n;i++) res+=cnt[vc[i]];
	return res%10;
}
int main(){
	init
	cin>>n>>m;
	for(int i=1;i<N*N;i++) p[i]=i,cnt[i]=1;
	for(int i=1;i<=n;i++) cin>>g[i]+1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(g[i][j]=='.'&&!vis[i][j]){
				bfs(i,j);
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(g[i][j]=='.') cout<<".";
			else cout<<solve(i,j);
		}
		cout<<"\n";
	}
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vic.GoodLuck

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值