Bear and Square Grid

枚举k * k 的位置, 然后接上它周围白色连通块的数量, 再统计完全在k * k范围里的连通块, 这个只要某个连通块全部的方格

在k * k里面就好, 并且k * k是一行一行移的, 所以可以优化到n ^ 3。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, k, idx, now, id[N][N], sum[N][N];
bool vis[N * N];
char Map[N][N]; int fa[N * N], cnt[N * N], num[N * N]; int getRoot(int x) {
return fa[x] == x ? x : fa[x] = getRoot(fa[x]);
} void change(int x, int op) {
x = getRoot(x);
if(op == ) {
num[x]--;
if(!num[x]) now += cnt[x];
} else {
if(!num[x]) now -= cnt[x];
num[x]++;
}
}
int main() {
scanf("%d%d", &n, &k);
for(int i = ; i <= n * n; i++) fa[i] = i;
for(int i = ; i <= n; i++)
scanf("%s", Map[i] + );
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(Map[i][j] == '.') id[i][j] = ++idx, cnt[idx] = ;
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
if(Map[i][j] != '.') continue;
if(Map[i - ][j] == '.') {
int x = getRoot(id[i][j]);
int y = getRoot(id[i - ][j]);
if(x != y) fa[y] = x, cnt[x] += cnt[y];
}
if(Map[i][j - ] == '.') {
int x = getRoot(id[i][j]);
int y = getRoot(id[i][j - ]);
if(x != y) fa[y] = x, cnt[x] += cnt[y];
}
}
}
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
sum[i][j] = sum[i - ][j] + sum[i][j - ] - sum[i - ][j - ] + (Map[i][j] == 'X');
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(Map[i][j] == '.') num[getRoot(id[i][j])]++; int ans = ;
queue<int> que; for(int i = ; i + k - <= n; i++) {
for(int j = ; j + k - <= n; j++) {
if(j == ) {
for(int u = i; u < i + k; u++)
for(int v = j; v < j + k; v++)
change(id[u][v], );
}
int ret = ;
if(i - > ) {
for(int z = j; z <= j + k - ; z++) {
if(Map[i - ][z] != '.') continue;
int x = getRoot(id[i - ][z]);
if(!vis[x]) {
vis[x] = true;
que.push(x);
ret += cnt[x];
}
}
}
if(i + k <= n) {
for(int z = j; z <= j + k - ; z++) {
if(Map[i + k][z] != '.') continue;
int x = getRoot(id[i + k][z]);
if(!vis[x]) {
vis[x] = true;
que.push(x);
ret += cnt[x];
}
}
} if(j - > ) {
for(int z = i; z <= i + k - ; z++) {
if(Map[z][j - ] != '.') continue;
int x = getRoot(id[z][j - ]);
if(!vis[x]) {
vis[x] = true;
que.push(x);
ret += cnt[x];
}
}
}
if(j + k <= n) {
for(int z = i; z <= i + k - ; z++) {
if(Map[z][j + k] != '.') continue;
int x = getRoot(id[z][j + k]);
if(!vis[x]) {
vis[x] = true;
que.push(x);
ret += cnt[x];
}
}
}
while(!que.empty()) {
vis[que.front()] = false;
que.pop();
}
ans = max(ans, now + ret + sum[i + k - ][j + k - ] - sum[i - ][j + k - ] - sum[i + k - ][j - ] + sum[i - ][j - ]);
if(j + k <= n) {
for(int z = i; z < i + k; z++) change(id[z][j], -);
for(int z = i; z < i + k; z++) change(id[z][j + k], );
} else {
for(int u = i; u < i + k; u++)
for(int v = j; v < j + k; v++)
change(id[u][v], -);
}
}
}
printf("%d\n", ans);
return ;
} /*
*/

Codeforces 679C Bear and Square Grid的更多相关文章

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

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

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

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

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

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

  5. Codeforces679C. Bear and Square Grid

    n<=500,n*n的01矩阵,可以选择一个k*k的矩阵全变1,求最大1联通区域. 敢敢n^3..模拟k*k的矩阵的位置,从左到右扫的时候,每变一个位置只会引起边界的信息变化,就记含边界的k*k ...

  6. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  7. Codeforces 385B Bear and Strings

    题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...

  8. Codeforces Round #448 C. Square Subsets

    题目链接 Codeforces Round #448 C. Square Subsets 题解 质因数 *质因数 = 平方数,问题转化成求异或方程组解的个数 求出答案就是\(2^{自由元-1}\) , ...

  9. Codeforces 680D Bear and Tower of Cubes 贪心 DFS

    链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...

随机推荐

  1. pygame(class类)调用视图的方法

    以下将介绍pygame精灵动画的基础知识,希望对大家有帮助:1.在此,精灵类必须继承pygame.sprite.Sprite并初始化pygame.sprite.Sprite.__init__(self ...

  2. linux C sscanf()函数

    linux sscanf() 类似正则表达式,又不完全是正则表达式. 分割 ”/“ 或 "@" 或空格 要用 [^/] 例如: sscanf("iios/12DDWDFF ...

  3. java工程师之旅-一个月工作心得

    不知不觉,在工作中已经度过一个月,距离上次写文章已经好几个月了,正好还有二十分钟下班,抽点时间来写一下博文,写一下心得. 首先说一下,在我工作之前,做了一个项目,和一个外校大四的学生做一个毕业设计,一 ...

  4. Ubuntu16.04+CUDA8.0+cudnn6

    按之前的方法给TITAN X安装cuda8.0会发生循环登录的问题,因此换了一种安装方法 参考:https://www.jianshu.com/p/002ece426793,http://blog.c ...

  5. jquery筛选数组之grep、each、inArray、map的用法及遍历son对象(转)

    grep [传入的参数为返回bool类型的函数] <script type='text/javascript' src="/jquery.js"></script ...

  6. ROC,AUC,PR,AP介绍及python绘制

    这里介绍一下如题所述的四个概念以及相应的使用python绘制曲线: 参考博客:http://kubicode.me/2016/09/19/Machine%20Learning/AUC-Calculat ...

  7. Node.jsでMySQLを使うメモ

    インストール npm install mysql コネクション var mysql = require('mysql'); var connection = mysql.createConnectio ...

  8. nagios系列(八)之nagios通过nsclient监控windows主机

    nagios通过nsclient监控windows主机 1.下载NSClient -0.3.8-Win32.rar安装在需要被监控的windows主机中 可以设置密码,此处密码留空 2.通过在nagi ...

  9. 大道至简、大智若愚—GO语言最佳详解实践

      导读:2007年,受够了C++煎熬的Google首席软件工程师Rob Pike纠集Robert Griesemer和Ken Thompson两位牛人,决定创造一种新语言来取代C++, 这就是Gol ...

  10. jquery里面的$.each()方法

    $.each可以迭代jquery对象和数组 $(selector).each()专注于jquery对象的遍历