F. Elongated Matrix
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a matrix aa, consisting of nn rows and mm 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,…,snms1,s2,…,snm.
The traversal is kk-acceptable if for all ii (1≤i≤nm−11≤i≤nm−1) |si−si+1|≥k|si−si+1|≥k.
Find the maximum integer kk such that there exists some order of rows of matrix aa that it produces a kk-acceptable traversal.
Input
The first line contains two integers nn and mm (1≤n≤161≤n≤16, 1≤m≤1041≤m≤104, 2≤nm2≤nm) — the number of rows and the number of columns, respectively.
Each of the next nn lines contains mm integers (1≤ai,j≤1091≤ai,j≤109) — the description of the matrix.
Output
Print a single integer kk — the maximum number such that there exists some order of rows of matrix aa that it produces an kk-acceptable traversal.
Examples
input
Copy
4 2
9 9
10 8
5 3
4 3
output
Copy
5
input
Copy
2 4
1 2 3 4
10 3 7 3
output
Copy
0
input
Copy
6 1
3
6
2
5
1
4
output
Copy
3
Note
In the first example you can rearrange rows as following to get the 55-acceptable traversal:
5 3
10 8
4 3
9 9
Then the sequence ss will be [5,10,4,9,3,8,3,9][5,10,4,9,3,8,3,9]. Each pair of neighbouring elements have at least k=5k=5 difference between them.
In the second example the maximum k=0k=0, any order is 00-acceptable.
In the third example the given order is already 33-acceptable, you can leave it as it is.
因为n<=16 并且只能移动行,列是固定的。那么考虑状态压缩。
预处理每个行之间相邻的差值(这个与行的先后顺序无关)和第一行与最后一行的差值(这个与先后顺序有关)
dp[sta][top][now]
sta中1表示用过的行,0表示未使用的行,top表示第一行,now表示当前枚举到的行。
第一个枚举量为首行元素,然后依次加入其他行,维护行与行之间差值的最小值,ans取max即可。
dfs感觉会好写一点,记忆化一下,状态最多也就1e7大概吧
这次div3的题目不难。。感觉都会做,但就是莫名其妙的卡题理论AK,写完的代码全是BUG天才BUG选手,代码能力还是太弱了。。。
#include "bits/stdc++.h"
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
int a[20][10004];
int dis[20][20];
int tr[20][20];
int dp[1<<16][20][20];
int n,m;
int dfs(int sta,int top,int now)
{
if(sta==(1<<n)-1){//末行与首行的差值
return tr[now][top];//注意顺序不可以变
}
if(dp[sta][top][now]!=-1)return dp[sta][top][now];
int ans=0;
for (int i = 0; i < n; ++i) {
int x=1<<i;
if(!(sta&x))
{
ans=max(ans, min(dfs( sta|x,top,i), dis[i][now]) );
}
}
return dp[sta][top][now]=ans;
}
int main()
{
scanf("%d%d",&n,&m);
memset(dp,-1, sizeof(dp));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
scanf("%d",&a[i][j]);
}
}
for (int i = 0; i < n; ++i) {//预处理
for (int j = 0; j < n; ++j) {
int mini=inf;
for (int k = 0; k < m; ++k) {
mini=min(mini,abs(a[i][k]-a[j][k]));
}
dis[i][j]=mini;
mini=inf;
for (int k = 0; k < m-1; ++k) {
mini=min(mini,abs(a[i][k]-a[j][k+1]));
}
tr[i][j]=mini;
}
}
int ans=0;
for (int i = 0; i < n; ++i) {//枚举首行元素
ans=max(ans,dfs(1<<i,i,i));
}
printf("%d\n",ans);
}