题意:给你个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(前缀和)题解的更多相关文章

  1. Codeforces 838A - Binary Blocks(二维前缀和+容斥)

    838A - Binary Blocks 思路:求一下前缀和,然后就能很快算出每一小正方块中1的个数了,0的个数等于k*k减去1的个数,两个的最小值就是要加进答案的值. 代码: #include< ...

  2. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

  3. Codeforces 547C/548E - Mike and Foam 题解

    目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...

  4. Codeforces Round #557 (Div. 1) 简要题解

    Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...

  5. CodeForces 1251B --- Binary Palindromes

    [CodeForces 1251B --- Binary Palindromes] Description A palindrome is a string t which reads the sam ...

  6. Codeforces Round #665 (Div. 2)A-C题解

    A. Distance and Axis 题目:http://codeforces.com/contest/1401/problem/A 题解:对于n来说分两种情况,一是奇数,二则是偶数 ①奇数:对于 ...

  7. Codeforces Round #668 (Div. 2)A-C题解

    A. Permutation Forgery 题目:http://codeforces.com/contest/1405/problem/A 题解:这道题初看有点吓人,一开始居然想到要用全排序,没错我 ...

  8. Codeforces Round #669 (Div. 2)A-C题解

    A. Ahahahahahahahaha 题目:http://codeforces.com/contest/1407/problem/A 题解:最多进行n/2的操作次数,我们统计这n个数中1的个数,是 ...

  9. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

随机推荐

  1. 为listview的item中的元素设置onclick事件

    表达能力比较差,所以现在解释一下标题的意思:listview的列表项,点击的时候触发的是itemOnClick事件,点击后转向到A页:那么,假如在子项中有一个连接是想转到B页,我们该怎么办呢.这样能明 ...

  2. 微信小程序 --- loading提示框

    loading:提示框: 效果: loading和toast和像,只不过 toast 是设置结束时间,时间到了去触发bindchange事件,进行隐藏. 但是 loading 是没有办法设置事件让其隐 ...

  3. backup与recover

    完全恢复: 1.关闭DB2.拷贝文件3.启动DB.<出错>startup mount4.recover database until cancel using backup control ...

  4. KM算法(最优匹配)

    hdu2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. Hibernate之核心文件

    一个Hibernate项目核心配置文件主要分为以下三个方面:1.配置文件hibernate.cfg.xml:2.配置文件*.hbm.xml,此文件一般包括映射文件的结构以及各种属性的映射方式:3.Hi ...

  6. Java导出Excel表,POI 实现合并单元格以及列自适应宽度(转载)

    POI是apache提供的一个读写Excel文档的开源组件,在操作excel时常要合并单元格,合并单元格的方法是: sheet.addMergedRegion(new CellRangeAddress ...

  7. open live writer 安装 markdown 插件

    我自己用的是 OpenLiveWriter ,所以本篇只讲 OLW 的,WindowsLiveWriter 戳 MarkdownInWindowsLiveWriter,OpenLiveWriter 戳 ...

  8. (2.9)Mysql之SQL基础——索引的查看与删除

    (2.9)Mysql之SQL基础——索引的查看与删除 关键词:mysql索引查看,mysql索引删除 1.索引查询(以下包括主键,唯一,普通,复合,全文,但不包括外键) (1)按库查询 select ...

  9. loadNibNamed:(NSString *)name owner:(nullable id)owner options:(nullable NSDictionary *)options用法

    1.name xib的名字 owner当前类对象 options初始参数 实际应用: NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@&quo ...

  10. 翻译:Addressing tiles: same tile bounds with different indexes

    原文链接:http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/ Addressing tiles: same ...