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 ...
随机推荐
- jQuery的基础跟JS的正则
大家好,我是唯芸星,这是我的一点点学过的知识,呈现给大家 1:正则表达式 包括: 1:正则表达式包括两部分 ①:定义正则表达式的规则 ②:正则表达式的规模(i/g/ ...
- 几个常用EL表达式的用法
转载至 http://yqsshr.blog.51cto.com/469059/131824 1,用来获取表单数据 param 和 paramValues 1.jsp 的有如下表单 <for ...
- startsWith和endWith方法
startsWith(): 例如:if(a.startsWith(b)) //判断字符串a 是不是以字符串b开头. 语法1 public boolean startsWith(String prefi ...
- 深入剖析java迭代器以及C#迭代器!
目录: 知道迭代器接口Iterable 为什么java的for增强可以自动迭代 那些类可以被迭代 通过什么方法迭代 1.知道迭代器接口Iterable 解析: 迭代器(iterator)是一种对象,它 ...
- 转:【Java集合源码剖析】Java集合框架
转载轻注明出处:http://blog.csdn.net/ns_code/article/details/35564663 Java集合工具包位于Java.util包下,包含了很多常用的数据结构, ...
- 超级简单实用的前端必备技能-javascript-全屏滚动插件
fullPage.js fullPage.js是一个基于jQuery的全屏滚动插件,它能够很方便.很轻松的制作出全屏网站 本章内容将详细介绍Android事件的具体处理及常见事件. 主要功能 支持 ...
- 【Alpha阶段】第一次Scrum Meeting!
每日任务 1.本次会议为第一次 Meeting会议: 2.本次会议在中午12:30,在第五社区5号楼楼下,召开本次会议为30分钟讨论接下来的任务: 一.今日站立式会议照片 二.每个人的工作 (有wor ...
- 201521123036 《Java程序设计》第6周学习总结
本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 1.2 可选:使用常规方法总结其他上课内容. 对象克隆:Clon ...
- 201521123106 《Java程序设计》第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 finally 题目4-2 1.1 截图你的提交结果(出现学 ...
- 201521123066 《Java程序设计》第十二周实验总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...