Codeforces 679C 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的更多相关文章
- codeforces 680E Bear and Square Grid 巧妙暴力
这个题是个想法题 先预处理连通块,然后需要用到一种巧妙暴力,即0变1,1变0,一列列添加删除 复杂度O(n^3) #include <cstdio> #include <iostre ...
- 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\) 不超 ...
随机推荐
- FlowLayout OnSizeChanged
在FlowLayout里加了20个控件,当窗口变化时,改变这20个控件的宽高,结果发现在直接点最大化时, 计算不正确导致自身的滚动条出不来.把改变大小的代码直接添加Form窗口的onSizeChagn ...
- Linux 重启网卡失败 Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
linux下重启网卡使用命令 : service network restart 时报错: [root@slave01 hadoop]# service network restart Startin ...
- sql 行变列
select * from market//查看原来所有数据 //第一种方式 select max(case area when '南京' then num else 0 end) 南京, max(c ...
- Linux - 文件ACL权限控制
getfacl 1.test # 查看文件ACL权限 setfacl -R -m u:xuesong:rw- 1.test # 对文件增加用户的读写权限 -R 递归
- Zabbix 监控服务
熟悉了解一些 zabbix 基础项目监控 zabbix_get 相关操作 :获取 item 监控数据 基本格式: -s --host: 指定客户端主机名或者IP -p --port:客户端端口,默认 ...
- 8、判断三角形ABC中是否有点D
思路: 首先连接AD,BD,CD,SABC为三角形的面积,SABD为三角形ABD的面积,SACD....,SBCD....... 因此,若D在三角形则SABC = SABD + SACD + SBCD ...
- tomcat server.xml
基于对server.xml的学习,结合源码,可以进一步理解tomcat的架构设计 1. 2. 3. 4 .valve链 参考: http://www.importnew.com/17124.html ...
- python - 练习(获取windows硬件信息)
import subprocess import re # info = subprocess.Popen("systeminfo",shell=True,stdout=subpr ...
- Principal components analysis(PCA):主元分析
在因子分析(Factor analysis)中,介绍了一种降维概率模型,用EM算法(EM算法原理详解)估计参数.在这里讨论另外一种降维方法:主元分析法(PCA),这种算法更加直接,只需要进行特征向量的 ...
- Linux注销&登陆
⒈注销 ①在命令行使用logout,此指令在图形界面无效.