• http://codeforces.com/contest/838/problem/A


    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
    3 5
    00100
    10110
    11001
    output
    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.

     题意:给你一个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+5; 
    const int inf=0x3f3f3f3f; 
    int mp[maxn][maxn]; 
    char a[maxn]; 
    int n,m; 
    int main() 
    {
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",a+1); 
            for(int j=1;j<=m;j++)
            {
                if(a[j]=='1') 
                mp[i][j]=1; 
            } 
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                mp[i][j]=mp[i-1][j]+mp[i][j-1]+mp[i][j]-mp[i-1][j-1]; 
            } 
        } 
        int ans=inf;
        int len=max(n,m); 
        for(int k=2;k<=len;k++) 
        {
            int nn=n,mm=m; 
            if(n%k!=0)nn=(n/k)*k+k; 
            if(m%k!=0)mm=(m/k)*k+k;
             int tmp1=0; 
            for(int i=1;i<=nn/k;i++) 
            for(int j=1;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+1>n?n:i*k-k+1;
                int y2=j*k-k+1>m?m:j*k-k+1; 
                int tmp=mp[x1][y1]+mp[x2-1][y2-1]-mp[x1][y2-1]-mp[x2-1][y1];
                if(tmp<=k*k/2)
                {
                    tmp1+=(tmp); 
                } 
                else
                {
                    tmp1+=(k*k-tmp); 
                } 
            } 
            ans=min(ans,tmp1); 
        } 
        
        printf("%d
    ",ans); 
    } 
  • 相关阅读:
    解决web服务器乱码问题
    Reporting services 打印
    moss用户管理
    乱弹超级女声。。。。。。。。。。。。
    微软项目管理[EPM]数据库应用举例1: 找到所有正在进行的项目
    ajax中另一种装载数据页面的方法
    微软项目管理[EPM]数据库剖析4:项目大纲代码的四张表
    微软项目管理[EPM]数据库应用举例2: 取得一个项目的某大纲代码的值
    支持多表头、滚动条可排序的DataGrid控件[Free]
    微软项目管理[EPM]数据库剖析3:如何取得某个项目的某个大纲代码的值
  • 原文地址:https://www.cnblogs.com/lhclqslove/p/7308400.html
Copyright © 2020-2023  润新知