cf div3 F. Elongated Matrix(状压dp)

35 篇文章 0 订阅
10 篇文章 0 订阅

F. Elongated Matrix

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a matrix aa, consisting of nn rows and mm columns. Each cell contains an integer in it.

You can change the order of rows arbitrarily (including leaving the initial order), but you can't change the order of cells in a row. After you pick some order of rows, you traverse the whole matrix the following way: firstly visit all cells of the first column from the top row to the bottom one, then the same for the second column and so on. During the traversal you write down the sequence of the numbers on the cells in the same order you visited them. Let that sequence be s1,s2,…,snms1,s2,…,snm.

The traversal is kk-acceptable if for all ii (1≤i≤nm−11≤i≤nm−1) |si−si+1|≥k|si−si+1|≥k.

Find the maximum integer kk such that there exists some order of rows of matrix aa that it produces a kk-acceptable traversal.

Input

The first line contains two integers nn and mm (1≤n≤161≤n≤16, 1≤m≤1041≤m≤104, 2≤nm2≤nm) — the number of rows and the number of columns, respectively.

Each of the next nn lines contains mm integers (1≤ai,j≤1091≤ai,j≤109) — the description of the matrix.

Output

Print a single integer kk — the maximum number such that there exists some order of rows of matrix aa that it produces an kk-acceptable traversal.

Examples

input

Copy

4 2
9 9
10 8
5 3
4 3

output

Copy

5

input

Copy

2 4
1 2 3 4
10 3 7 3

output

Copy

0

input

Copy

6 1
3
6
2
5
1
4

output

Copy

3

Note

In the first example you can rearrange rows as following to get the 55-acceptable traversal:

5 3
10 8
4 3
9 9

Then the sequence ss will be [5,10,4,9,3,8,3,9][5,10,4,9,3,8,3,9]. Each pair of neighbouring elements have at least k=5k=5 difference between them.

In the second example the maximum k=0k=0, any order is 00-acceptable.

In the third example the given order is already 33-acceptable, you can leave it as it is.

题意:给出一个n*m的矩阵,将矩阵按列遍历的顺序拆成一个数列,你可以任意的交换行的位置,找到一个最大的k使得|s[i]-s[i+1]|<=k

题解:因为n很小,可以枚举起点,然后枚举过程点和终点来进行状压dp,dp[s][i]表示s这个状态,i是最后到达的点的答案,首先应该请客所有的状态,然后给只有起点状态赋最大值,这样就会确定起点

#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e7+7;
typedef long long ll;
const int mod=998244353;
int read(){int c = getchar(),Nig = 1,x = 0;while(!isdigit(c))c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()

int a[20][10005],dp[1<<18][20],dis[20][10005];
int Sheryang()
{
    int n=read,m=read;

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            a[i][j]=read;
        }
    }

    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            int tmp=0x3f3f3f3f;
            for(int k=0;k<m;k++)
                tmp=min(tmp,abs(a[i][k]-a[j][k]));
            dis[i][j]=dis[j][i]=tmp;
        }
    }

    int ans=0;
    for(int s=0;s<n;s++){
        for(int j=0;j<(1<<n);j++)
            fill_n(dp[j],n,0);
        dp[1<<s][s]=0x3f3f3f3f;
        for(int i=0;i<(1<<n);i++){
            for(int j=0;j<n;j++){
                if(dp[i][j]){
                    for(int k=0;k<n;k++)
                    if(!(i&(1<<k))){
                       dp[i|(1<<k)][k]=max(dp[i|(1<<k)][k],min(dp[i][j],dis[j][k]));
                    }
                }
            }
        }
        for(int i=0;i<n;i++){
            int k=dp[(1<<n)-1][i];
            for(int j=0;j+1<m;j++)
                k=min(k,abs(a[i][j]-a[s][j+1]));
            ans=max(ans,k); 
        }
    }

    cout<<ans<<endl;
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值