题目链接: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. 评估“不合格”!教育部暂停山东大学、复旦大学、南京师范大学部分硕士、博士学位授权点...
  2. 机械爪的带有压力反馈的控制实验
  3. win下手动编译狂魔的必备C/C++编译环境
  4. ICCV 2017 CREST:《CREST: Convolutional Residual Learning for Visual Tracking》论文笔记
  5. PowerPC VxWorks BSP分析(1)--PowerPC体系结构
  6. 【国际专场】laravel多用户平台(SaaS, 如淘宝多用户商城)的搭建策略
  7. leaf 叶子(张量)
  8. android rxjava2 简书,RXJava2学习
  9. JAVA编码(27)——执行批量导入Excel文件并进行解析
  10. RG-AP220-E
  11. JAVA NumberFormat和DecimalFormat小结
  12. 教师教育网各网页链接
  13. 能破解百度网盘提取码,云盘万能钥匙宣布关闭!
  14. 16.第二十二章.信息安全管理
  15. 单片机c语言编写音乐播放器,51单片机c语言编写电子琴+音乐播放器.doc.doc
  16. 机器人工程专业学习金字塔
  17. 【机器学习】训练集、验证集与测试集
  18. 一文读懂,CPU、精简指令集、复杂指令集该如何理解?
  19. 巨量指数signature
  20. matlab处理数学物理方法,MATLAB在数学物理方法中的应用

热门文章

  1. 时间序列预测 深度学习_从时间序列到深度学习的销售预测
  2. AI产品经理视角下的AI翻译机 in 旅游场景
  3. 201521123091 《Java程序设计》第11周学习总结
  4. 「经济/商学/理财」简说
  5. Docker 安装及镜像加速器配置
  6. 9014,9013,8050三极管引脚图与管脚识别方法
  7. javaweb——jsp动作标签
  8. 计算机学院学生会招新宣传语,团学招新 | 计算机学院团委、学生会招新啦!(一)...
  9. Win11怎么删除微软输入法?
  10. Linux学习之计划任务(at、batch、crontab)篇