传送门:https://codeforces.com/contest/1102/problem/F
You are given a matrix a, consisting of n rows and m 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,…,snm.

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

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

Input

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

Each of the next n lines contains m integers (1≤ai,j≤109) — the description of the matrix.

Output

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

Examples

input

4 2
9 9
10 8
5 3
4 3

output

5

题意:给一个n*m的数字矩阵,可以随意交换任意两行的顺序,然后按列顺序遍历整个矩阵,得到一个长度为nm的序列。要求使任意交换行之后的矩阵得到的序列中相邻两个数差值的最小值最大。
解法:忽略行,把每一行看做一个单独的节点,如果i行与j行相邻,那么他们之间有一个权值,大小为min(abs(a[i][k] - a[j][k])), k表示列,取值为1~m;对于第1行和第n行,他们之间也有权值,假如让i行作为第1行,j行作为第n行,那么他们之间的权值就为min(abs(a[i][k], a[j][k+1])), k的取值为1~m-1 。
如果我们忽略第1行与第n行之间的权值,那么这个题就变成了求最小哈密顿距离,直接dp就好。考虑第1行与第n行之间的权值,就需要枚举第1行和第n行。

/*
c1[i][j]存的是i,j行相邻时的权值
c2[i][j]存的是当第i行作为最后一行,j行作为第一行时的权值
*/
#include<bits/stdc++.h>using namespace std;
const int maxn = 1e4 + 5;
const int inf = 0x3f3f3f3f;
int a[17][maxn], c1[17][17], c2[17][17], dp[1 << 17][17];
int n, m;
int cal(int mask, int u)
{if(dp[mask][u] != -1) return dp[mask][u];dp[mask][u] = 0;for(int v = 0; v < n; v++){if(v == u) continue;if((mask >> v) & 1)dp[mask][u] = max(dp[mask][u], min(c1[v][u], cal(mask ^ (1 << u), v)));}return dp[mask][u];
}
int main()
{ios::sync_with_stdio(false);cin >> n >> m;for(int i = 0; i < n; i++){for(int j = 0; j < m; j++) cin >> a[i][j];}for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){c1[i][j] = c2[i][j] = inf;for(int k = 0; k < m; k++) c1[i][j] = min(c1[i][j], abs(a[i][k] - a[j][k]));for(int k = 0; k < m - 1; k++) c2[i][j] = min(c2[i][j], abs(a[i][k] - a[j][k + 1]));}}int ans = 0;// 这里就是在枚举第1行和第n行for(int i=0;i<n;i++){memset(dp,-1,sizeof dp);for(int j=0;j<n;j++)dp[1<<j][j]=i==j?inf:0;  // 这里的inf就保证了i一定是第1行for(int j=0;j<n;j++)ans=max(ans,min(c2[j][i],cal((1<<n)-1,j)));}cout << ans << '\n';return 0;
}

cf 1102F Elongated Matrix相关推荐

  1. CodeForces - 1102F Elongated Matrix(哈密顿路径+状压dp)

    题目链接:点击查看 题目大意:给出一个 n∗mn * mn∗m 的数字矩阵,现在可以对 行 进行重新排列,现在对排列后的矩阵按列展开成线性:s1,s2,-,snm=maze1,1,maze2,1,.. ...

  2. Strassen算法

    $$\left[ \begin{matrix} A ,B\\ C,D \end{matrix} \right] \times \left[ \begin{matrix} E,F\\ G,H \end{ ...

  3. [GCN] 图卷积知识梳理 -持续更新

    图卷积知识梳理 文章目录 图卷积知识梳理 1. 为什么 Graph Laplacian L=D−AL=D-AL=D−A --差分的方式理解 2. 为什么是 Graph Laplacian LLL -- ...

  4. paper reading:[renormalization]Semi-supervised Classification with Graph Convolutional Networks

    paper reading:[Renormalization Trick] Semi-supervised classification with graph convolutional networ ...

  5. 2020_KDD_Dual Channel Hypergraph Collaborative Filtering

    [论文阅读笔记]2020_KDD_Dual Channel Hypergraph Collaborative Filtering 论文下载地址: https://doi.org/10.1145/339 ...

  6. 数据挖掘课笔记(八)

    以下笔记来自于学堂在线上清华大学的视频网课<80240372X 数据挖掘:理论与算法>,本笔记仅用于个人学习.如有错误,感谢指正. 推荐算法 关于"推荐":例如搜索引擎 ...

  7. CF 1475 F . Unusual Matrix 思维

    传送门 大体题意:给定两个矩阵a和b,给定一个操作,这个操作可以将a矩阵任意一行或者任意一列取反,问能否将a变成b. 乍一看不是一个很难的题,但是想我这样思维不好的还是看不出来什么东西.让后看到了题解 ...

  8. cf #213 Matrix

    题目:http://codeforces.com/contest/365/problem/C 题目分析: sum(x,y,z,t)=s(x,y)*s(z,t),s(x,y)=s[x]+s[x+1]+. ...

  9. CF 390D:Inna and Sweet Matrix

    这题也不好解释.大意是一个n*m的矩阵,I有k枚糖果,每一枚糖果都会逐一放在(i , j)的格子上,且I从(1,1)到这个格子必须存在一条没有糖果覆盖的路径,否则无法放置.求I放完所有糖果后所需的最小 ...

  10. Matrix Studio LeetCode 刷题指南

    Hello 大家好,我是Alex,今天来说明一下Matrix工作室每日一题的刷题指南,虽然刷题一直饱受诟病,很多人不想刷题,但不可否认刷题确实能锻炼我们的编程能力,相信每个认真刷题的人都会有体会. 现 ...

最新文章

  1. 【CV知识学习】early stop、regularation、fine-tuning and some other trick to be known
  2. 如何删除一个员工编号及其全部主数据和事务数据?
  3. 大四中软实习笔记20130226
  4. 基于bboss开发平台eclipse开发工程生成工具介绍
  5. 从前序与中序遍历序列构造二叉树Python解法
  6. 怎样学好C++ ----高手的话
  7. 系统架构师学习笔记-数据库系统
  8. jQuery框架学习第八天:ASP.NET jQuery实施方案
  9. 栈的输出_算法:栈和队列题目集合(一)
  10. 系统理解Win32 API和MFC
  11. Windows下编译redis
  12. C++与STL简单介绍( C/C++机试)
  13. python设计模式
  14. 电容触摸屏测试软件,大规模生产中如何测量触摸屏电容值
  15. 电脑如何压缩图片大小kb?如何在线压缩图片?
  16. 高德地图API调用自定义地图使用
  17. Cocos2d-JS 中游戏背景音乐与音效
  18. 信号与系统--冲激响应
  19. 阿里携“骑呗”入局共享单车,好戏即将开始
  20. 如何用 LiquidText 高效阅读分析文献?

热门文章

  1. 前端根据后端数据生成表格 行列合并 指定表头
  2. JAVA常用加密解密算法Encryption and decryption
  3. GPT+UEFI双硬盘双系统安装
  4. java中graphics_在java中如何绘图?Graphics类是什么意思?
  5. Android学习笔记(四十):Preference的使用
  6. 红警3命令与征服注册激活启动cdkey联机问题
  7. notimplementedexception
  8. JQuery EasyUI 结合ztrIee的后台页面开发
  9. 怎样使用计算机定时关机,如何设置电脑每天定时关机?电脑设置定时关机的方法...
  10. Win11怎么删除微软输入法?