CodeForces 1102F. Elongated Matrix 状压Dp

Portal

The meaning of problems

You give a \ (n \ times m (n \ leq 16) \) matrix, you can adjust the order of the rows, and a one, are numbered from top to bottom for each element
provided \ (k \ leq | s_i- + {I}. 1 S_ | \) , requires \ (K \) maximum.

Thinking

First preprocessed at a position corresponding to the maximum element of absolute value of the difference between any two lines,
as well as the first and last line of this particular position of the absolute values of difference between the maximum
and the whole arrangement of violence found timeout
because \ (n-\ ) very small pressure can be shaped,
the largest minimum difference absolute value I set f [17] [17] [ 200000] in order to meet the number of the first row is selected, the currently selected row number, the selected numberIn fact, the first line number can not go to remember
Then you can run a search memory,Too hard to write a dynamic return

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int MAXN=1e4+10;
int n,m,mt[20][MAXN];
int as[20][20],bs[20][20],ans;
int f[17][17][200000];

inline int dfs(int pos,int first,int last,int stu){
    if(pos==n+1){
        return bs[first][last];
    }
    if(f[first][last][stu]!=-1) return f[first][last][stu];
    for(int i=1;i<=n;i++){
        if((stu&(1<<i))==0){
            f[first][last][stu]=max(f[first][last][stu],min(dfs(pos+1,first,i,stu|(1<<i)),as[i][last]));
        }
    }
    return f[first][last][stu];
}

int main(){
//  freopen("data.in","r",stdin);
//  freopen("data.out","w",stdout);
    memset(as,0x3f,sizeof(as));
    memset(bs,0x3f,sizeof(bs));
    memset(f,-1,sizeof(f));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&mt[i][j]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            for(int k=1;k<=m;k++){
                as[i][j]=min(as[i][j],abs(mt[i][k]-mt[j][k]));
                if(k<m) bs[i][j]=min(bs[i][j],abs(mt[i][k+1]-mt[j][k]));
            }
    for(int i=1;i<=n;i++)
        ans=max(ans,dfs(2,i,i,1<<i));
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/BakaCirno/p/12164534.html