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的更多相关文章

  1. 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 ...

  2. Codeforces 679C Bear and Square Grid

    Bear and Square Grid 枚举k * k 的位置, 然后接上它周围白色连通块的数量, 再统计完全在k * k范围里的连通块, 这个只要某个连通块全部的方格 在k * k里面就好, 并且 ...

  3. 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 ...

  4. codeforces 680E Bear and Square Grid 巧妙暴力

    这个题是个想法题 先预处理连通块,然后需要用到一种巧妙暴力,即0变1,1变0,一列列添加删除 复杂度O(n^3) #include <cstdio> #include <iostre ...

  5. CF679C(Bear and Square Grid) 经典好题

    题目链接:传送门 题目大意:给你一个n*n包含".","X"的图,你有一次机会选择一个k*k的子矩阵,将子矩阵全部变为".",问当操作过后, ...

  6. Codeforces Round #356 (Div. 2)

    A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. uva 11520 - Fill the Square

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  8. いろはちゃんとマス目 / Iroha and a Grid (组合数学)

    题目链接:http://abc042.contest.atcoder.jp/tasks/arc058_b Time limit : 2sec / Memory limit : 256MB Score ...

  9. 洛谷P2867 [USACO06NOV]大广场Big Square

    P2867 [USACO06NOV]大广场Big Square 题目描述 Farmer John's cows have entered into a competition with Farmer ...

随机推荐

  1. 472 Concatenated Words 连接的单词

    详见:https://leetcode.com/problems/concatenated-words/description/ C++: class Solution { public: vecto ...

  2. API系列一:REST和RESTful认识

    序言 最近工作的项目一直使用API,就想趁這个机会,把API的知识点进行一次梳理和总结,顺便提升一下自己对API全新的认识 Web API 是ASP.NET平台新加的一个特性,它可以简单快速地创建We ...

  3. HTML5应用缓存与Web Workers

    1.什么是应用程序缓存      HTML5引入了应用程序缓存,这意味着web应用可进行缓存,并可在没有因特网链接时进行访问. 2.应用缓存的优势      离线浏览   用户可在应用离线时使用它们 ...

  4. VS2012创建WebForm项目提示错误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。

    解决方案: 使用VS2012开发,都要装NuGet插件(官网:http://nuget.org),进官网点安装就进入了微软的下载页面, 选择vs2012版本的NuGet.Tools.vsix文件,双击 ...

  5. Collection接口框架图

    Java集合大致可分为Set.List和Map三种体系,其中Set代表无序.不可重复的集合:List代表有序.重复的集合:而Map则代表具有映射关系的集合.Java 5之后,增加了Queue体系集合, ...

  6. vim设置默认显示行号

    vim /root/.vimrc 设置在当前登录用户根目录下,.vimrc文件本身不存在,创建后之间添加下面配置保存即可 set number

  7. opencv-flag

    http://blog.csdn.net/yiyuehuan/article/details/43701797 在Mat类中定义了这样一个成员变量: /*! includes several bit- ...

  8. 安卓 Android 简单数据库(增删改查)

    <Button android:id="@+id/delete_btn" android:layout_width="wrap_content" andr ...

  9. 常见的HTTP相应状态码

    200:请求被正常处理204:请求被受理但没有资源可以返回206:客户端只是请求资源的一部分,服务器只对请求的部分资源执行GET方法,相应报文中通过Content-Range指定范围的资源.301:永 ...

  10. github 添加完sshkey之后仍然需要输入密码

    1.在家目录下创建.netrc文件,内容如下 machine github.com login username password password window下创建:在用户文件夹如C:\Users ...