Codeforces679C. Bear and Square Grid
n<=500,n*n的01矩阵,可以选择一个k*k的矩阵全变1,求最大1联通区域。
敢敢n^3。。模拟k*k的矩阵的位置,从左到右扫的时候,每变一个位置只会引起边界的信息变化,就记含边界的k*k矩形内的各联通块的大小以及不含边界的k*k的矩形内的0的个数,然后边移动边开个桶更新。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
//#include<vector>
//#include<iostream>
using namespace std; int n,K;
#define maxn 511
char mp[maxn][maxn];
int id[maxn][maxn]; int ufs[maxn*maxn],size[maxn*maxn];
int find(int x) {return x==ufs[x]?x:(ufs[x]=find(ufs[x]));}
void Union(int x,int y) {x=find(x),y=find(y); if (x==y) return; size[y]+=size[x]; ufs[x]=y;} int bud[maxn*maxn]; bool vis[maxn][maxn];
int quex[maxn*maxn],quey[maxn*maxn],head,tail;
const int dx[]={,-,,},dy[]={,,,-};
void bfs(int x,int y)
{
vis[x][y]=; int ff=id[x][y];
head=; tail=; quex[]=x; quey[]=y;
while (head!=tail)
{
const int xx=quex[head],yy=quey[head]; head++;
for (int i=;i<;i++)
{
int nx=xx+dx[i],ny=yy+dy[i];
if (nx< || nx>n || ny< || ny>n || vis[nx][ny] || mp[nx][ny]!='.') continue;
vis[nx][ny]=; quex[tail]=nx; quey[tail]=ny; tail++;
int idid=id[nx][ny]; Union(idid,ff);
}
}
} void insert(int x,int y,int &tot)
{
int now=id[x][y],ff=find(now);
if (!bud[ff]) tot+=size[ff]; bud[ff]++;
}
void Delete(int x,int y,int &tot)
{
int now=id[x][y],ff=find(now);
bud[ff]--; if (!bud[ff]) tot-=size[ff];
}
int main()
{
scanf("%d%d",&n,&K);
for (int i=;i<=n;i++) scanf("%s",mp[i]+);
for (int i=,tot=;i<=n;i++)
for (int j=;j<=n;j++)
id[i][j]=++tot;
for (int i=;i<=n*n;i++) ufs[i]=i,size[i]=; for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (mp[i][j]=='.' && !vis[i][j]) bfs(i,j);
int ans=;
for (int i=K;i<=n;i++)
{
memset(bud,,sizeof(bud));
int tot=,xx=;
for (int j=i-K+;j<=i;j++)
for (int k=;k<=K;k++) if (mp[j][k]=='.') insert(j,k,tot);
else xx++;
if (K<n) for (int j=i-K+;j<=i;j++) if (mp[j][K+]=='.') insert(j,K+,tot);
if (i>K) for (int k=;k<=K;k++) if (mp[i-K][k]=='.') insert(i-K,k,tot);
if (i<n) for (int k=;k<=K;k++) if (mp[i+][k]=='.') insert(i+,k,tot);
ans=max(ans,tot+xx); for (int k=K+;k<=n;k++)
{
for (int j=i-K+;j<=i;j++) xx-=(mp[j][k-K]=='X');
for (int j=i-K+;j<=i;j++) xx+=(mp[j][k]=='X');
if (k>K+) for (int j=i-K+;j<=i;j++) if (mp[j][k-K-]=='.') Delete(j,k-K-,tot);
if (k<n) for (int j=i-K+;j<=i;j++) if (mp[j][k+]=='.') insert(j,k+,tot);
if (i>K)
{
if (mp[i-K][k]=='.') insert(i-K,k,tot);
if (mp[i-K][k-K]=='.') Delete(i-K,k-K,tot);
}
if (i<n)
{
if (mp[i+][k]=='.') insert(i+,k,tot);
if (mp[i+][k-K]=='.') Delete(i+,k-K,tot);
}
ans=max(ans,tot+xx);
// cout<<i<<' '<<k<<' '<<tot<<' '<<xx<<endl;
}
}
printf("%d\n",ans);
return ;
}
Codeforces679C. Bear and Square Grid的更多相关文章
- Codeforces Round #356 (Div. 1) C. Bear and Square Grid
C. Bear and Square Grid time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 679C Bear and Square Grid
Bear and Square Grid 枚举k * k 的位置, 然后接上它周围白色连通块的数量, 再统计完全在k * k范围里的连通块, 这个只要某个连通块全部的方格 在k * k里面就好, 并且 ...
- Codeforces Round #356 (Div. 2) E. Bear and Square Grid 滑块
E. Bear and Square Grid 题目连接: http://www.codeforces.com/contest/680/problem/E Description You have a ...
- codeforces 680E Bear and Square Grid 巧妙暴力
这个题是个想法题 先预处理连通块,然后需要用到一种巧妙暴力,即0变1,1变0,一列列添加删除 复杂度O(n^3) #include <cstdio> #include <iostre ...
- CF679C(Bear and Square Grid) 经典好题
题目链接:传送门 题目大意:给你一个n*n包含".","X"的图,你有一次机会选择一个k*k的子矩阵,将子矩阵全部变为".",问当操作过后, ...
- Codeforces Round #356 (Div. 2)
A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- uva 11520 - Fill the Square
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- いろはちゃんとマス目 / Iroha and a Grid (组合数学)
题目链接:http://abc042.contest.atcoder.jp/tasks/arc058_b Time limit : 2sec / Memory limit : 256MB Score ...
- 洛谷P2867 [USACO06NOV]大广场Big Square
P2867 [USACO06NOV]大广场Big Square 题目描述 Farmer John's cows have entered into a competition with Farmer ...
随机推荐
- SP CAML工具
直接一直使用CAML做一些简单的SP列表查询,突然想对CAML进一步了解,于是找到两个常用工具,做以记录: 1 Caml Query Builder : 用于编写CAML查询,对初学者可以了解查询语句 ...
- Effective Java读书笔记完结啦
Effective Java是一本经典的书, 很实用的Java进阶读物, 提供了各个方面的best practices. 最近终于做完了Effective Java的读书笔记, 发布出来与大家共享. ...
- HTML中的那些bug
1.语法检测时提示有多余的结束标签 <!doctype html> <html> <head> <meta charset="utf-8" ...
- Objective-C Memory Management 内存管理 2
Objective-C Memory Management 内存管理 2 2.1 The Rules of Cocoa Memory Management 内存管理规则 (1)When you c ...
- C#方法参数关键字
一.params关键字 prams告诉函数的调用者,该函数的参数数量是可变,如果调用函数的参数标识了params关键字,那么我们可以使用逗号分割的参数或者一个数组来作为参数: 1.这里只能是数组,Li ...
- Node.js——fs常用API
文件状态 文件删除 文件信息 读取文件夹 文件的截取 创建文件夹 删除目录 文件监视,可以设置监视频率 文件重命名,可以用来剪切文件 注意 fs.open() fs.close() 这是最原始的读写方 ...
- 数据字典Dictionary存放键值对
1. 方法思路: 使用数据字典[Dictionary<string, string>],声明一个list集合,将“XML子节点名称”.“节点值”以键[节点名称]值[节点值]对的形式 ...
- 【译】x86程序员手册41-10.6 TLB(快表)测试
译注:本章基本未做翻译 10.6 TLB Testing TLB测试 The 80386 provides a mechanism for testing the Translation Lookas ...
- ZooKeeper系列(三)
前面虽然配置了集群模式的Zookeeper,但是为了方面学建议在伪分布式模式的Zookeeper学习Zookeeper的shell命令. 一.Zookeeper的四字命令 Zookeeper支持某些特 ...
- 优雅的创建map/list集合
带值的集合的创建 String[] a = {"1","2","3","4"}; boolean q = ArrayUt ...