Sereja and Table CodeForces - 425B (思维枚举)

本文介绍了一种通过枚举方式解决特殊矩阵变换问题的方法。该问题要求通过有限次改变单元格值使矩阵满足特定条件:相同值的连通区域形成平行于边界的矩形。文章详细解释了如何利用二进制枚举来确定最优解,并提供了实现这一解决方案的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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\n");
    else
    {
        printf("%d\n",MIN);
    }
    return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值