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. web.py 模板错误记录

    错误信息 Traceback (most recent call last): File , in process return self.handle() File , in handle retu ...

  2. 使用Eclipse Memory Analyzer 进行JAVA内存泄露分析

    一,安装 Eclipse Memory Analyzer 在Memory Analyzer的官网找到 update site的地址:

  3. mssql拿webshell的方法

    首先检测下MSSQL数据库的用户权限,一般有两种,一种是SA权限,这种权限很大,还有一种是DB_OWNER权限,这个权限赋给用户一些对数据库的修改.删除.新增数据库表,执行部分存储过程的权限.但是涉及 ...

  4. UVA565 Pizza Anyone? (状态压缩,搜索)

    UVA565 Pizza Anyone? 大致题意:现在你要做一份披萨,有A到P共16种食材.现在给你1~12个人对这个披萨加入不同食材的条件(只包含想要和不想要两种)(加号是想要,减号是不想要,不一 ...

  5. 字符加密 Valentino 函数 (伪分治)

    题面 \(solution:\) 这一题重点不在字符串加密,而是我们最后的求值:\(K^{s}\mod M\)(\(s\leq36^{100000}\)) 而我们发现它的指数十分巨大,但众所周知的指数 ...

  6. Prometheus 监控 Nginx 流量 (三)

    介绍 基于Openresty和Prometheus.Consul.Grafana设计的,实现了针对域名和Endpoint级别的流量统计,使用Consul做服务发现.KV存储,Grafana做性能图展示 ...

  7. hashMap之jdk1.7和jdk1.8

    参考链接: http://allenwu.itscoder.com/hashmap-analyse https://tech.meituan.com/java-hashmap.html

  8. Java InputStream 、 InputStreamReader和BufferedReader

    https://blog.csdn.net/zgljl2012/article/details/47267609 在Java中,上述三个类经常用于处理数据流,下面介绍一下三个类的不同之处以及各自的用法 ...

  9. android okhttp的使用

    OkHttpClient client = new OkHttpClient(); String url = ""; Request request = new Request.B ...

  10. Django开发笔记一

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.运行 python manage.py runser ...