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\) 不超 ...
随机推荐
- POJ3250Bad Hair Day
http://poj.org/problem?id=3250 题意 : n个牛排成一列向右看,牛 i 能看到牛 j 的头顶,当且仅当牛 j 在牛 i 的右边并且牛 i 与牛 j 之间的所有牛均比牛 ...
- 2013 Multi-University Training Contest 1 Cards
数据不是很大,直接枚举约数,判断4个条件是否满足! 这样就得到4种卡片,总共2^4种情况,枚举各种情况即可!!! #include<iostream> #include<cmath& ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- lintcode 中等题:N Queens N皇后问题
题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...
- SDK 与MFC
SDK 就是Software Development Kit 软件开发包MFC 就是Microsoft Foundation Classes 微软函数类库.是以C++类的形式封装了Windows的AP ...
- iOS iOS7越狱
1.使用盘古越狱工具 (或者PP助手) 2.越狱成功后需要安装Apple File Conduit “2”,用于替代afc2add插件 3.安装AppSync插件 (绕过系统验证,随意安装.运行破解的 ...
- JAVA+ Proxool+ SQLserver 2008 “signer information does not match signer information of other classes in the same package”
1. Proxool+SQLserver2008(sqljdbc4.jar)集成问题最近在项目中遇到个问题:我用的是Proxool连接池,连接SQLserver2008数据库,控制台报错:签名信息和同 ...
- html5--等待加载效果
<!DOCTYPE HTML> <html lang="zh-cmn-Hans"> <head> <meta charset=" ...
- Linux进程的睡眠和唤醒简析
COPY FROM:http://www.2cto.com/os/201204/127771.html 1 Linux进程的睡眠和唤醒 在Linux中,仅等待CPU时间的进程称为就绪进程,它们被放置在 ...
- php程序员应具有的7种能力
php程序员应具有什么样的能力,才能更好的完成工作,才会有更好的发展方向呢?在中国我想您不会写一辈子代码的,那样不可能,过了黄金期,您又怎么办呢?看了本文后,希望对您有所帮助. 一,php能力 1,了 ...