Codeforces:F - Elongated Matrix

题目链接: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;
}
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值