cf 1102F Elongated Matrix

本文探讨了一道算法题目,要求在给定的n*m数字矩阵中,通过调整行顺序来最大化相邻元素差值的最小值。解决方案涉及构建图模型,使用动态规划求解最小哈密顿距离,特别关注于处理第一行与最后一行之间的特殊权值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门: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;
}
### 将0805封装电阻适配到0603封装的设计方法 在电子设计领域,当需要将较大的0805封装电阻焊接至较小的0603封装PCB焊盘时,可以通过调整焊盘布局以及优化焊接工艺来实现这一目标。以下是具体的技术细节: #### 1. 焊盘尺寸调整 为了适应0805封装电阻的物理尺寸,可以适当增大原本用于0603封装的焊盘面积[^2]。通常情况下,0805封装的长度为2.0mm,宽度为1.25mm;而0603封装则分别为1.6mm和0.8mm。因此,在不影响其他元件布置的前提下,应扩展焊盘之间的距离以匹配0805的实际尺寸。 ```plaintext |-------------------| | | <- 增大后的焊盘间距 (约2.0 mm) |___________________| ``` #### 2. 调整焊盘形状 除了增加焊盘间的间隔外,还可以通过修改焊盘的具体几何形态进一步提高兼容性。例如采用椭圆形或者加长矩形代替标准方形焊盘能够更好地贴合较大体积组件底部金属触点区域[^3]。 #### 3. 使用助焊剂增强连接质量 由于存在一定程度上的不完全接触情况(即原生针对更小型号所设定),推荐在整个组装过程中施加适量液态助焊膏于各端子位置之上从而促进熔融锡料流动并形成牢固可靠的电气机械联结结构[^4]。 #### 4. 手动微调与机器辅助相结合 对于批量生产的场景来说,则需依赖精密拾放设备完成精准定位操作后再送入回流炉加热固化流程;而对于少量样品制作而言, 则可能更多依靠手工烙铁工具来进行细致调控直至达到预期效果为止. ```python def adjust_resistor_placement(resistor_size="0805", target_pcb_footprint="0603"): """ Adjust resistor placement based on size and footprint. Args: resistor_size (str): The actual resistor package size ("0805"). target_pcb_footprint (str): Target PCB design footprint ("0603"). Returns: str: Adjustment recommendation message. """ if resistor_size != "0805" or target_pcb_footprint != "0603": return f"No adjustment needed between {resistor_size} and {target_pcb_footprint}." adjustments = [ "Increase pad spacing to match the larger dimensions of 0805.", "Modify pad shapes using elongated rectangles or ovals for better alignment." ] return "\n".join(adjustments) result = adjust_resistor_placement() print(result) ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值