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 ...
随机推荐
- Linux平台 Oracle 12cR2 RAC安装Part1:准备工作
Linux平台 Oracle 12cR2 RAC安装Part1:准备工作 一.实施前期准备工作 1.1 服务器安装操作系统 1.2 Oracle安装介质 1.3 共享存储规划 1.4 网络规范分配 二 ...
- Centos 7 PXE一键安装
author:JevonWei 版权声明:原创作品 192.168.198.134作为安装服务器,由httpd服务共享安装程序 192.168.198.134作为dhcp服务器,客户机获取IP 一.安 ...
- Cassandra HBase和MongoDb性能比较
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp68这是一篇基于亚马逊云平台上对三个主流的NoSQL数据库性能比较,在读写 ...
- Java代理详解
一.概述 代理模式是Java常用的设计模式之一,实现代理模式要求代理类和委托类(被代理的类)具有相同的方法(提供相同的服务),代理类对象自身并不实现真正的核心逻辑,而是通过调用委托类对象的相关方法来处 ...
- js模拟点击事件实现代码
js模拟点击事件实现代码 类型:转载 时间:2012-11-06 在实际的应用开发中,我们会常常用到JS的模事件,比如说点击事件,举个简单的例子,点击表单外的"提交"按钮来提交表单 ...
- SNS团队Beta阶段第七次站立会议(2017.5.28)
1.立会照片 2.每个人的工作 成员 今天已完成的工作 罗于婕 对界面各部分的图标进行完善.美化 龚晓婷 对于历史记录功能进一步完善 林仕庄 对于历史记录功能进一步完善 刘海兰 协调界面的整体美化 念 ...
- 【Alpha】Daily Scrum Meeting——Day2
站立式会议照片 1.本次会议为第二次 Meeting会议: 2.本次会议在中午12:30,在陆大楼召开,本次会议为30分钟讨论昨天的任务完成情况以及接下来的任务安排. 每个人的工作分配 成 员 昨天已 ...
- 201521123053 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 答:我开始做笔记了,在本周学习中的一些笔记 * abstract关键字是为了实现 ...
- 201521123108 《Java程序设计》第4周学习总结
1. 本章学习总结 2. 书面作业 Q1. 注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) 答: Q2. 面向对象设计(大作业1-非常重要) 2. ...
- python类型转换、数值操作(收藏)
最近学习python语言,碰到数据类型间的转换问题.看到一篇文章总结的挺详细,收藏之备用. 类型转换 代码 1 函数 描述 2 int(x [,base ...