http://codeforces.com/contest/838/problem/A
2 seconds
256 megabytes
standard input
standard output
You are given an image, that can be represented with a 2-d n by m grid of pixels. Each pixel of the image is either on or off, denoted by the characters "0" or "1", respectively. You would like to compress this image. You want to choose an integer k > 1 and split the image into k by k blocks. If n and m are not divisible by k, the image is padded with only zeros on the right and bottom so that they are divisible by k. Each pixel in each individual block must have the same value. The given image may not be compressible in its current state. Find the minimum number of pixels you need to toggle (after padding) in order for the image to be compressible for some k. More specifically, the steps are to first choose k, then the image is padded with zeros, then, we can toggle the pixels so it is compressible for this k. The image must be compressible in that state.
The first line of input will contain two integers n, m (2 ≤ n, m ≤ 2 500), the dimensions of the image.
The next n lines of input will contain a binary string with exactly m characters, representing the image.
Print a single integer, the minimum number of pixels needed to toggle to make the image compressible.
3 5
00100
10110
11001
5
We first choose k = 2.
The image is padded as follows:
001000
101100
110010
000000
We can toggle the image to look as follows:
001100
001100
000000
000000
We can see that this image is compressible for k = 2.
题意:给你一个n*m的0,1表,然后让你找到一个整数K去划分这张图,然后可以改变每个划分的图的任意的值,使每一部分的的值相同
题解:枚举k,维护0,1表的前缀和,去快速计算每个分块的总和;下面代码
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream>
#define ll long long
using namespace std;
const int maxn=2e3+5e2+;
const int inf=0x3f3f3f3f;
int mp[maxn][maxn];
char a[maxn];
int n,m;
int main()
{
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%s",a+);
for(int j=;j<=m;j++)
{
if(a[j]=='')
mp[i][j]=;
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
mp[i][j]=mp[i-][j]+mp[i][j-]+mp[i][j]-mp[i-][j-];
}
}
int ans=inf;
int len=max(n,m);
for(int k=;k<=len;k++)
{
int nn=n,mm=m;
if(n%k!=)nn=(n/k)*k+k;
if(m%k!=)mm=(m/k)*k+k;
int tmp1=;
for(int i=;i<=nn/k;i++)
for(int j=;j<=mm/k;j++)
{
int x1=i*k>n?n:i*k;
int y1=j*k>m?m:j*k;
int x2=i*k-k+>n?n:i*k-k+;
int y2=j*k-k+>m?m:j*k-k+;
int tmp=mp[x1][y1]+mp[x2-][y2-]-mp[x1][y2-]-mp[x2-][y1];
if(tmp<=k*k/)
{
tmp1+=(tmp);
}
else
{
tmp1+=(k*k-tmp);
}
}
ans=min(ans,tmp1);
} printf("%d\n",ans);
}
http://codeforces.com/contest/838/problem/A的更多相关文章
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环
E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...
- http://codeforces.com/contest/555/problem/B
比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...
- http://codeforces.com/contest/610/problem/D
D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/612/problem/D
D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- http://codeforces.com/contest/536/problem/B
B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/535/problem/C
C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- http://codeforces.com/contest/402/problem/E
E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...
- codeforces.com/contest/251/problem/C
C. Number Transformation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
随机推荐
- adb 安装apk 报错:Failure [INSTALL_FAILED_INVALID_URI]
今天在安装某apk的时候,发现报错 报错信息:Failure [INSTALL_FAILED_INVALID_URI] 于是找到了一个解决方式: cmd命令行下执行以下命令: 第一步.adb remo ...
- 10分钟学会ES7+ES8
撰文为何 身为一个前端开发者,ECMAScript(以下简称ES)早已广泛应用在我们的工作当中.了解ECMA机构流程的人应该知道,标准委员会会在每年的6月份正式发布一次规范的修订,而这次的发布也将作为 ...
- java常量池中基本数据类型包装类的小陷阱
想必大部分学过java的人都应该做过这种题目: public class Test { public static void main(String[] args) { //第一个字符串 String ...
- SessionStateMode之Redis共享session
周六的时候用SQL Server来实现session共享,今天下班早就试了下使用Redis来实现session共享.接着上一篇博客,上一篇使用sessionState节点mode="SQLS ...
- bootstrap 基础(二)
1 栅格系统偏移 栅格系统的偏移只能向右:col-xs-offset-x. <!DOCTYPE html> <html lang="en"> <hea ...
- Makefile中export分析
在分析内核启动过程的./arch/arm/Makefile文件里碰到了这样字段 162 export TEXT_OFFSET GZFLAGS MMUEXT 然后在子目录arch/arm/kernel/ ...
- 用shell批量编码转换
-------------------------------------文件内容转换:iconv-------------------------------------- 通常,从其他平台拷贝过来 ...
- 团队作业4——第一次项目冲刺 FiFtH DaY
项目冲刺--Penta Kill 很开心,小编今天LOL也拿到了五杀,感觉自己又可以去吹一年了. 不扯这些有的没的了,让我们来看看今天的任务吧~ Mission 看起来好像和昨天没有什么不同哦,其实是 ...
- 201521123017 《Java程序设计》第8周学习总结
1. 本周学习总结 2. 书面作业 Q1.List中指定元素的删除(题目4-1) 1.1 实验总结 for (int i = list.size()-1; i >=0; i--) {//从最后一 ...
- 201521123077 《Java程序设计》第4周学习总结
1. 本周学习总结 几种简单说明注释的使用 抽象类与抽象方法 super调用父类的方法 2. 书面作业 Q1.注释的应用使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看. ...