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.

猜你喜欢

转载自blog.csdn.net/qq_41568836/article/details/81916618