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\)条边,使得 ...
随机推荐
- java FileUtil(文件操作类)
package tools; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; i ...
- tomcat的添加及jar包和jQuery的加载
- tomcat配置JMX
最近看JDK的命令行工具,使用Java VisualVM和Jconsole工具都可以监控java程序的运行情况(包括CUP和内存等的使用情况,线程的运行状态等) 在Java VisualVM 工具里可 ...
- Java.lang的研究(分析包含的重要类和接口)
Java.lang包是Java中使用最广泛的一个包,它包含很多定义的类和接口. java.lang包包括以下这些类: Boolean Byte Character Class ClassLoader ...
- postgresql----根据现有表创建新表
除普通的建表语句"create table table_name(columns);"之外,还可以根据现有表快速的创建新表: 一.使用create table ... (like ...
- Oracle体系结构之Oracle分区
目录 Oracle分区 0 一.Oracle分区理论知识 1 二.分区表的实现方式 1 1.范围分区(range partition table) 1 2.列表分区(list partitioning ...
- ETL__pentaho__SPOON_PDI
Pentaho Data Integration (PDI, also called Kettle),是pentaho的etl工具.虽然etl工具一般都用在数据仓库环境中,可是,PDI还是可以做以下事 ...
- Linux自动化部署尝试
Linux自动化部署尝试 最近做一个项目临近测试,购买的是阿里云的服务器,每次部署都是手动打包war,然后上传到服务器,然后修改配置文件,不仅繁琐,而且费时,就思索着找一个一键式的部署方式,今天终 ...
- D. Babaei and Birthday Cake---cf629D(LIS线段树优化)
题目链接:http://codeforces.com/problemset/problem/629/D 题意就是现有n个蛋糕,蛋糕的形状是圆柱体,每个蛋糕的体积就是圆柱体的体积,每个蛋糕的编号是1-- ...
- 【PHP】善用php-fpm的慢执行日志slow log,分析php性能问题
(转)善用php-fpm的慢执行日志slow log,分析php性能问题 众所周知,mysql有slow query log,根据慢查询日志,我们可以知道那些sql语句有性能问题.作为mysql的好 ...