zoukankan      html  css  js  c++  java
  • Sereja and Table CodeForces

    Sereja has an n × m rectangular table a, each cell of the table contains a zero or a number one. Sereja wants his table to meet the following requirement: each connected component of the same values forms a rectangle with sides parallel to the sides of the table. Rectangles should be filled with cells, that is, if a component form a rectangle of size h × w, then the component must contain exactly hwcells.

    A connected component of the same values is a set of cells of the table that meet the following conditions:

    • every two cells of the set have the same value;
    • the cells of the set form a connected region on the table (two cells are connected if they are adjacent in some row or some column of the table);
    • it is impossible to add any cell to the set unless we violate the two previous conditions.

    Can Sereja change the values of at most k cells of the table so that the table met the described requirement? What minimum number of table cells should he change in this case?

    Input

    The first line contains integers nm and k (1 ≤ n, m ≤ 100; 1 ≤ k ≤ 10). Next n lines describe the table a: the i-th of them contains mintegers ai1, ai2, ..., aim (0 ≤ ai, j ≤ 1) — the values in the cells of the i-th row.

    Output

    Print -1, if it is impossible to meet the requirement. Otherwise, print the minimum number of cells which should be changed.

    Examples

    Input

    5 5 2
    1 1 1 1 1
    1 1 1 1 1
    1 1 0 1 1
    1 1 1 1 1
    1 1 1 1 1
    

    Output

    1
    

    Input

    3 4 1
    1 0 0 0
    0 1 1 1
    1 1 1 0
    

    Output

    -1
    

    Input

    3 4 1
    1 0 0 1
    0 1 1 0
    1 0 0 1
    

    Output

    0

    思维枚举

    题意不是很好理解

    此题看一眼就知道枚举

    问题就是怎样枚举

    如何枚举才不会超时呢?

    那么我们就发现了没必要枚举所有的情况

    为什么呢

    就是因为题中的k比较小

    当n>k是那么必有一行是无法反转的

    所以就枚举每一行的状态进行比较就可以了

    这里小于10的部分我运用了二进制

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <cstdlib>
    #include <queue>
    #include <stack>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <set>
    using namespace std;
    int a[105][105];
    int cmp[105];
    int main()
    {
        int n,m,w;
        scanf("%d%d%d",&n,&m,&w);
        if(n>m)
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    scanf("%d",&a[i][j]);
                }
            }
        }//n代表列反转行列
        else
        {
            swap(n,m);
            for(int i=1;i<=m;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    scanf("%d",&a[j][i]);
                }
            }
        }
    //    for(int i=1;i<=n;i++)
    //    {
    //        for(int j=1;j<=m;j++)
    //        {
    //           printf("%d ",a[i][j]);
    //        }
    //        cout<<endl;
    //    }
       int MIN=1e9;
        if(n<=10)
        {
            for(int i=0;i<=pow(2,m)-1;i++)
            {
                int tmp=i;
                for(int j=1;j<=m;j++)
                {
                    cmp[j]=tmp&1;
                    tmp=tmp>>1;
    
                }
                int ans=0;
                for(int j=1;j<=n;j++)
                {
                    int cnt=0;
                    for(int k=1;k<=m;k++)
                    {
                        if(a[j][k]==cmp[k])
                        {
                            cnt++;
                        }
                    }
                    ans+=min(cnt,m-cnt);
                }
                MIN=min(ans,MIN);
            }
        }
        else
        {
            for(int i=1;i<=n;i++)
            {
                int ans=0;
                for(int j=1;j<=n;j++)
                {
                    int cnt=0;
                    for(int k=1;k<=m;k++)
                    {
                        if(a[i][k]==a[j][k])
                        {
                            cnt++;
                        }
                    }
                    ans+=min(cnt,m-cnt);
                }
            MIN=min(MIN,ans);
            }
        }
        if(MIN>w)
            printf("-1
    ");
        else
        {
            printf("%d
    ",MIN);
        }
        return 0;
    
    }
    
  • 相关阅读:
    利用子查询解决复杂sql问题
    如何用临时表代替游标进行表记录的拷贝
    SQL新函数, 排名函数 ROW_NUMBER(), RANK(), DENSE_RANK()
    SQL SERVER2005 中的错误捕捉与处理
    用户自定义函数代替游标进行循环拼接
    使用游标进行循环数据插入
    Oracle中利用存储过程建表
    SQL SERVER中强制类型转换cast和convert的区别
    SQL如何修改被计算字段引用的字段类型
    1.官方网站:http://www.mplayerhq.hu/design7/dload.html
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852302.html
Copyright © 2011-2022 走看看