CodeForces 838A Binary Blocks(前缀和)题解
题意:给你个n*m的矩阵,要求你找到一个k,k > 1,使得矩阵可以分为很多k * k的小正方形,然后进行操作把每个小正方形都变为0或1,问你怎样使操作数最小。
思路:随便暴力不可取,显然你每次遍历查找k * k正方形里1和0的数量会超时。这里新学了一招前缀和,其实和二位树状数组差不多。就是预处理前缀和,以达到花O(1)的时间算出(1,1)~(x,y)的和,这样我们就能直接暴力枚举每一种k的操作数,然后取最小即为答案。
参考:前缀和与差分
代码:
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = +;
const int seed = ;
const int MOD = ;
const int INF = 0x3f3f3f3f;
int sum[maxn][maxn];
char s[maxn];
int get(int x1,int y1,int x2,int y2){
return sum[x2][y2] - sum[x2][y1 - ] - sum[x1 - ][y2] + sum[x1 - ][y1 - ];
}
int main(){
int n,m,k,end;
scanf("%d%d",&n,&m);
end = max(n,m);
memset(sum,,sizeof(sum));
for(int i = ; i <= n; i++){
scanf("%s",s + );
for(int j = ; j <= m; j++){
sum[i][j] = s[j] - '';
}
}
for(int i = ;i < maxn;i++){ //前缀和
for(int j = ;j < maxn;j++){
sum[i][j] += sum[i][j - ] + sum[i - ][j] - sum[i - ][j - ];
}
}
int ans = INF;
for(int k = ; k <= end; k++){
int cnt = ,endL,endR;
if(n % k == ) endL = n;
else endL = (n / k + ) * k;
if(m % k == ) endR = m;
else endR = (m / k + ) * k; for(int i = ; i <= endL; i += k){
for(int j = ; j <= endR; j += k){
int have = get(i,j,i + k - ,j + k - );
cnt += min(have,k * k - have);
}
}
ans = min(ans,cnt);
}
printf("%d\n",ans);
return ;
}
CodeForces 838A Binary Blocks(前缀和)题解的更多相关文章
- Codeforces 838A - Binary Blocks(二维前缀和+容斥)
838A - Binary Blocks 思路:求一下前缀和,然后就能很快算出每一小正方块中1的个数了,0的个数等于k*k减去1的个数,两个的最小值就是要加进答案的值. 代码: #include< ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces 547C/548E - Mike and Foam 题解
目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- CodeForces 1251B --- Binary Palindromes
[CodeForces 1251B --- Binary Palindromes] Description A palindrome is a string t which reads the sam ...
- Codeforces Round #665 (Div. 2)A-C题解
A. Distance and Axis 题目:http://codeforces.com/contest/1401/problem/A 题解:对于n来说分两种情况,一是奇数,二则是偶数 ①奇数:对于 ...
- Codeforces Round #668 (Div. 2)A-C题解
A. Permutation Forgery 题目:http://codeforces.com/contest/1405/problem/A 题解:这道题初看有点吓人,一开始居然想到要用全排序,没错我 ...
- Codeforces Round #669 (Div. 2)A-C题解
A. Ahahahahahahahaha 题目:http://codeforces.com/contest/1407/problem/A 题解:最多进行n/2的操作次数,我们统计这n个数中1的个数,是 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
随机推荐
- JS-利用ajax获取json数据,并传入页面生成动态tab
封装好的:ajax.js function ajax(url, fnSucc,fnFaild){ //1[创建] if(window.XMLHttpRequest){ var oAjax = new ...
- WCF(四) 绑定
绑定 是一个制定好的通道栈,包含了协议通道,传输通道和编码器.从功能上来看,一个绑定集成了通信模式.可靠性.安全性.事务传播和互操作性 绑定方式分两种:代码中和配置文件中绑定 1: 2: 3.配置ap ...
- mysql5.6主从
1.环境 操作系统:centos6.5(主服务器IP:192.168.100.170,从服务器IP:192.168.100.171)软件版本:mysql5.6.24 2.开始安装: a.主库上设置从库 ...
- Hadoop集群搭建文档
环境: Win7系统装虚拟机虚拟机VMware-workstation-full-9.0.0-812388.exe Linux系统Ubuntu12.0.4 JDK j ...
- Oracle等待事件之db file scattered read
1.产生原因 该等待事件通常发生在数据库多块读时,表示发生了与全表扫描和快速索引扫描相关的等待.通常意味着全表扫描过多,或者I/O 能力不足,或者I/O 竞争. 2.确定产生问题对象方法 a)查找全表 ...
- 三种空格unicode(\u00A0,\u0020,\u3000)表示的区别
1.不间断空格\u00A0,主要用在office中,让一个单词在结尾处不会换行显示,快捷键ctrl+shift+space ; 2.半角空格(英文符号)\u0020,代码中常用的; 3.全角空格(中文 ...
- django-base
1.django创建 2.django常用命令 3.django配置 一.创建django程序 1.终端:django-admin startproject sitename 2.IDE创建Djang ...
- 以太坊api访问,区块同步监测
以太坊geth api访问,区块同步监测 curl查询geth区块高度 supervisor管理以太坊geth进程 geth进程健康检查 # curl访问geth api #使用curl访问geth ...
- 信号(Django信号、Flask信号、Scrapy信号)
简介 Django.Flask.scrapy都包含了一个“信号分配器”,使得当一些动作在框架的其他地方发生的时候,解耦的应用可以得到提醒. 通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒 ...
- android 本地通知
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notificat ...