codeforces 680E Bear and Square Grid 巧妙暴力
这个题是个想法题
先预处理连通块,然后需要用到一种巧妙暴力,即0变1,1变0,一列列添加删除
复杂度O(n^3)
#include <cstdio>
#include <iostream>
#include <ctime>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=5e2+;
const int INF=0x3f3f3f3f;
const int mod=1e9+;
char s[N][N];
int vis[N][N],sum[N][N],now[N*N],cnt,n,k,bcc[N*N];
int dx[]={,,-,};
int dy[]={-,,,};
void dfs(int x,int y){
vis[x][y]=cnt,++bcc[cnt];
for(int i=;i<;++i){
int nx=x+dx[i],ny=y+dy[i];
if(nx<||nx>n||ny<||ny>n||s[nx][ny]=='X'||vis[nx][ny])continue;
dfs(nx,ny);
}
}
int cur;
void del(int x,int y){
if(!vis[x][y])return;
if((--now[vis[x][y]])==)cur-=bcc[vis[x][y]];
}
void add(int x,int y){
if(!vis[x][y])return;
if((++now[vis[x][y]])==)cur+=bcc[vis[x][y]];
}
int cal(int x,int y){
return sum[x][y]-sum[x-k][y]-sum[x][y-k]+sum[x-k][y-k];
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;++i)
scanf("%s",s[i]+);
for(int i=;i<=n;++i)
for(int j=;j<=n;++j){
sum[i][j]=sum[i-][j]+sum[i][j-]-sum[i-][j-];
sum[i][j]+=s[i][j]=='.'?:;
}
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
if(s[i][j]=='.'&&!vis[i][j])++cnt,dfs(i,j);
int ret=;
for(int i=;i<=n-k+;++i){
cur=,memset(now,,sizeof(now));
for(int p=i-;p<=i+k;++p)
for(int q=;q<=k+;++q)
add(p,q);
del(i-,k+),del(i+k,k+);
ret=max(ret,cur+k*k-cal(i+k-,k));
for(int p=;p<=n-k+;++p){
for(int q=i;q<=i+k-;++q)
del(q,p-),add(q,p+k);
del(i-,p-);del(i+k,p-);
add(i-,p+k-);add(i+k,p+k-);
ret=max(ret,cur+k*k-cal(i+k-,p+k-));
}
}
printf("%d\n",ret);
return ;
}
codeforces 680E Bear and Square Grid 巧妙暴力的更多相关文章
- 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 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 ...
- CF679C(Bear and Square Grid) 经典好题
题目链接:传送门 题目大意:给你一个n*n包含".","X"的图,你有一次机会选择一个k*k的子矩阵,将子矩阵全部变为".",问当操作过后, ...
- Codeforces679C. Bear and Square Grid
n<=500,n*n的01矩阵,可以选择一个k*k的矩阵全变1,求最大1联通区域. 敢敢n^3..模拟k*k的矩阵的位置,从左到右扫的时候,每变一个位置只会引起边界的信息变化,就记含边界的k*k ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- Codeforces 385B Bear and Strings
题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...
- Codeforces Round #448 C. Square Subsets
题目链接 Codeforces Round #448 C. Square Subsets 题解 质因数 *质因数 = 平方数,问题转化成求异或方程组解的个数 求出答案就是\(2^{自由元-1}\) , ...
- Codeforces 680D Bear and Tower of Cubes 贪心 DFS
链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...
随机推荐
- jQuery从主页面存取控制 iframe 中的元素,参数及方法
从主页面上获取iframe下的某个对象,或使用iframe下的方法,或是获取iframe下某个doc元素,要求兼容各类浏览器,不仅仅ie; $(function() { $('#abgne_ifram ...
- 套题T7
P4712 铺瓷砖 时间: 1000ms / 空间: 65536KiB / Java类名: Main 描述
- PHP输入流php://input [转]
对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述. "php://input allows you to read raw POST data. It is ...
- LR_问题_如何将场景中的用户设置为百分比形式
一个场景运行多个脚本时,如何按照百分比模式运行
- Linux磁盘管理命令
1.磁盘分割: fdisk [root@linux ~]# fdisk [-l] 装置名称 参数: -l :输出后面接的装置所有的partition内容.若仅有fdisk -l时, 则系统将会把整个系 ...
- PCL—低层次视觉—点云分割(基于凹凸性)
1.图像分割的两条思路 场景分割时机器视觉中的重要任务,尤其对家庭机器人而言,优秀的场景分割算法是实现复杂功能的基础.但是大家搞了几十年也还没搞定——不是我说的,是接下来要介绍的这篇论文说的.图像分割 ...
- Servlet的应用
1.重定向 HttpServletRequest接口提供的sendRedirect()方法用于生产302响应码和Location响应头,从而通知客户端去重新访问Location响应头中指定的U ...
- web前端性能测试小点
关于前端性能的文章: http://www.cnblogs.com/fnng/archive/2011/09/19/2181894.html web应用的前端性能响应时间指浏览器的页面加载时间.浏览器 ...
- ios越狱开发第一次尝试记录
1.THEOS的makefile文件中的THEOS_DEVICE_IP要写在第一行 2.如果make package install报错 dpkg status database is locked ...
- inand和emmc区别
简单来说:inand式sandisk公司做的一款符合emmc标准的一个emmc存储器! ----