题意:给你个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. 图论之最短路径(2)——Bellman-Ford算法

    继续最短路径!说说Bellman—Ford算法 思路:假设起点为s,图中有n个顶点和m个边,那么它到任一点(比如i)的最短路径 最多可以有n-1条(没有回路就是n-1条):因为最短路径中不可能包含回路 ...

  2. String() 函数把对象的值转换为字符串。

    var test1 = new Boolean(1);var test2 = new Boolean(0);var test3 = new Boolean(true);var test4 = new ...

  3. 【BZOJ2157】旅游 树链剖分+线段树

    [BZOJ2157]旅游 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本 ...

  4. js中的颜色对应的常量代码code

    颜色的对照表 颜色 英文代码 形像颜色 HEX格式 RGB格式   LightPink 浅粉红 #FFB6C1 255,182,193   Pink 粉红 #FFC0CB 255,192,203   ...

  5. INSERT高级应用

    INSERT INTO departments VALUES (departments_seq.nextval, 'Entertainment', 162, 1400); INSERT INTO em ...

  6. KMP的next数组性质运用

    hdu2594 Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  7. 从零搭建 vue-cli 脚手架

    前言: 用了几次 vue-cli 做 vue 项目,感觉没什么大问题,虽然也没有用 vue-router 和 vuex .但是心里一直有个梗,就是最初的目录生成和配置文件,一直没动过,也不知道具体原理 ...

  8. linux 统计文件数量

    查找当前目录下compose文件的数量 ls -lr | grep "compose" | wc -l

  9. nginx 重定向

    不带www跳转www 1.301: return 301 http://www.xx.com$request_uri; 2.(1)rewrite ^(.*)$ http://www.xx.com$1 ...

  10. PL/SQL集合(一):记录类型(TYPE 类型名称 IS RECORD)

    记录类型 利用记录类型可以实现复合数据类型的定义: 记录类型允许嵌套: 可以直接利用记录类型更新数据. 传统操作的问题 对于Oracle数据类型,主要使用的是VARCHAR2.NUMBER.DATE等 ...