Binary Blocks 前缀和预处理CodeForces - 838A

给一个m*n的矩阵,将其分成几个k*k的小块,小块中必须全是1或者0,通过修改小块的值,求最少改多少次

求一下前缀和,然后就能很快算出每一小正方块中1的个数了,0的个数等于k*k减去1的个数,两个的最小值就是要加进答案的值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<vector>
#include<sstream>
#include<queue>
#define ll long long
#define PI 3.1415926535897932384626
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=5005;
int pre[maxn][maxn]={0};
int node[maxn][maxn];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
                {
                    scanf("%1d",&node[i][j]);
                    pre[i][j]=node[i][j];
                }
    for(int i=1;i<maxn;i++)
        for(int j=1;j<maxn;j++)
            pre[i][j]+=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1];
    int num;
    int ans;
    int temp=inf;
    int t,t0;
    for(int k=2;k<=max(m,n);k++){
        ans=0;
        for(int i=k;i<=n+k;i+=k){
            for(int j=k;j<=m+k;j+=k)
            {
                num=k*k;
                t=pre[i][j]-pre[i-k][j]-pre[i][j-k]+pre[i-k][j-k];
                t0=num-t;
                //cout<<num<<" "<<t<<" "<<t0<<endl;
                int ret=min(t,t0);
                ans+=ret;
            }
        }
    temp=min(ans,temp);
    }
    printf("%d\n",temp);
    }

}

A. Binary Blocks

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print a single integer, the minimum number of pixels needed to toggle to make the image compressible.

Example

input

Copy

3 5
00100
10110
11001

output

Copy

5

Note

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷丘-CODER

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值