题目链接:http://codeforces.com/contest/1102/problem/F

F - Elongated Matrix

time limit per test 4 seconds
memory limit per test 256 megabytes

Porblem Description

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≤10^4, 2≤nm) — the number of rows and the number of columns, respectively.

Each of the next n lines contains m integers (1≤ai,j≤10^9) — 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

Input
2 4
1 2 3 4
10 3 7 3

Output
0

Input
6 1
3
6
2
5
1
4

Output
3

Note

In the first example you can rearrange rows as following to get the 5-acceptable traversal:
5 3
10 8
4 3
9 9

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

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

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



解题心得:

  • 队友告诉我这是一个旅行商问题,但是我并不会,所以看了看了大佬怎么写的,然后自己写了一遍。
  • 首先看n和m的范围,n是16这就很明显的一个状压的标志了。整体思路就是状压+记忆化,因为复杂度的原因需要预处理每两行之间对应差的最小值。然后枚举第i行后面需要移动的所有状态。用dp[i][j]表示第i行j状态(如果j二进制状态位置为k的地方是0代表第k行需要移动到i行后面),然后递归到需要移动的第k行。


#include <bits/stdc++.h>
using namespace std;
const int maxn = 20;
const int maxm = 1e4+100;int num[maxn][maxm], va[maxn][maxm], va2[maxn][maxm], dp[maxn][(1<<17)];
int n, m;void init() {scanf("%d%d",&n, &m);for(int i=0;i<n;i++) {for (int j = 0; j < m; j++) {scanf("%d", &num[i][j]);}}for(int i=0;i<n;i++) {for(int j=0;j<n;j++) {va[i][j] = va2[i][j] = INT_MAX;for(int k=0;k<m;k++){va[i][j] = min(va[i][j], abs(num[i][k] - num[j][k]));if(k != 0)va2[i][j] = min(va2[i][j], abs(num[i][k] - num[j][k-1]));//拼接成一个数组之后,拼接中间产生的差值}}}
}int row;int dfs(int pre, int state) {if(state == ((1<<n)-1)) return va2[row][pre];//无法再交换行的未知if(dp[pre][state] != -1) return dp[pre][state];//这个状态曾经被查找过dp[pre][state] = 0;for(int i=0;i<n;i++) {if(state & (1<<i))continue;dp[pre][state] = max(dp[pre][state], min(dfs(i, (state|(1<<i))), va[pre][i]));//递归}return dp[pre][state];
}int main() {//freopen("1.in", "r", stdin);init();int ans = 0;for(row=0; row<n; row++) {memset(dp, -1, sizeof(dp));ans = max(ans, dfs(row, (1<<row)));//后面可以交换的所有状态}printf("%d", ans);return 0;
}

Codeforces:F - Elongated Matrix相关推荐

  1. codeforces:F. All Possible Digits【贪心 + 模拟进位】

    目录 题目截图 题目分析 ac code 总结 题目截图 题目分析 注意是只能再最后一位加 我们要使得0到p - 1都出现至少一次 统计出现的数字aset 考虑最后一位pivot 情况1:如果pivo ...

  2. Educational Codeforces Round 9 F. Magic Matrix 最小生成树

    F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...

  3. CodeForces - 1401 F Reverse and Swap(线段树, 区间翻转, 区间交换,清晰易懂)

    CodeForces - 1401 F Reverse and Swap(线段树, 区间翻转, 区间交换)   首先一共有四个操作,第一个和第四个都是线段树的基本操作,直接用线段树实现.      第 ...

  4. 图像检索:Fisher Information Matrix and Fisher Kernel

    罗纳德·费雪(Sir Ronald Aylmer Fisher, FRS,1890.2.17-1962.7.29),现代统计学与现代演化论的奠基者之一,安德斯·哈尔德称他是"一位几乎独自建立 ...

  5. scikit-learn:通过Non-negative matrix factorization (NMF or NNMF)实现LSA(隐含语义分析)...

    scikit-learn:通过Non-negative matrix factorization (NMF or NNMF)实现LSA(隐含语义分析) 之前写过两篇文章.各自是 1)矩阵分解的综述:s ...

  6. 如果你也会C#,那不妨了解下F#(1):F# 数据类型

    简单介绍 F#(与C#一样,念作"F Sharp")是一种基于.Net框架的强类型.静态类型的函数式编程语言. 可以说C#是一门包含函数式编程的面向对象编程语言,而F#是一门包含面 ...

  7. F#简明教程二:F#类型系统和类型推断机制

    [51CTO独家特稿]在上一篇教程<F#与函数式编程概述>中我们了解到F#和函数式编程的一些特点,更多关于F#语言和函数式编程的介绍可以参考51CTO之前对微软MVP赵颉老师的专访< ...

  8. c语言利用fun求最小值,c语言:请编写函数fun(),他的功能是:求f(0)到f(50)的最小值,已知:f(0)=f(1)=1,f(2)=0,f...

    #include #include int f(int n) { if (n == 0 || n == 1) { return 1; } if (n == 2) { return 0; } retur ...

  9. intelliJ IDE 打包出错:F:/InterlliJ IDEA/Demo/src/main/java/META-INF/MANIFEST.MF' already exists in VFS

    在多次打包guo'过程中,突然chu'出现了一个这种错误,为了以后的学习和复习,将此错误记录下来. 错误:F:/InterlliJ IDEA/Demo/src/main/java/META-INF/M ...

  10. 题目内容: 写一个将华氏温度转换成摄氏温度的程序,转换的公式是: °F = (9/5)*°C + 32 其中C表示摄氏温度,F表示华氏温度。 程序的输入是一个整数,表示华氏温度。输出对

    #题目内容: 写一个将华氏温度转换成摄氏温度的程序,转换的公式是: °F = (9/5)*°C + 32 其中C表示摄氏温度,F表示华氏温度. 程序的输入是一个整数,表示华氏温度.输出对应的摄氏温度, ...

最新文章

  1. linux命令vgdisplay提示权限不足,linux常用命令总结
  2. java从磁盘读取图片_java 怎样从磁盘读取图片文件
  3. android热更新插件,与Android热更新方案Amigo的再次接触
  4. PhotoSwipe 图片浏览插件使用方法
  5. ABAP 在被访问的程序中获取访问程序的全局变量
  6. Android—Bitmap图片大小计算、压缩与三级缓存
  7. Dataphin产品核心功能大图(六)发布中心:生产和开发隔离模式下的保护伞
  8. JavaSpring框架有哪些优势?
  9. 阿里云镜像加速Docker
  10. 拉勾数据分析岗数据分析报告
  11. 电子邮件系统是如何运作的?
  12. 红帽考试环境之RHCSA
  13. Java中常见的几种数组排序方法
  14. 2022年最新版初级商业数字营销师直通车题库
  15. 0.5mm的焊锡丝能吃多大电流_【高考必备】高考物理5大类型的实验要点整理,考前一定要看!...
  16. 融资租赁、直租回租傻傻分不清楚
  17. 使用服务器出现error:cannot connect to X server
  18. 垂暮黄昏——回顾CSP2021
  19. 中龍鸿业分享理财产品净值1是什么意思?依据净值怎样算预期收益
  20. 关于长尾应用的一些思考

热门文章

  1. 【方向盘】达到Linux第三阶段的常用命令笔记记录---PartⅠ
  2. 我的心酸求职之路:如果可以,去学Java、C,不要搞Python
  3. 带你搞懂朴素贝叶斯分类算法
  4. HDU 5855 Less Time, More profit(最大权闭合子图)
  5. 地图坐标转换-火星坐标
  6. 第一章 教育基础(05 小学教育研究)
  7. GDT,LDT,GDTR,LDTR 详解
  8. 离职原因该怎么回答?
  9. macOS 开发 - 使用 ScreenSaverView 制作屏幕保护程序
  10. Vue使用百度分享,组件切换、销毁分享失效的解决办法