• cf1102F. Elongated Matrix(状压dp)


    题意

    题目链接

    Sol

    (n leqslant 16)可以想到状压

    我们可以预处理出任意两行之间每列的最小值以及相邻两列的最小值

    然后枚举一个起点,(f[sta][i])表示走过了(sta)这个集合内的元素,当前在(i)点的(k)的最大值

    转移的时候枚举接下来走哪个位置即可

    时间复杂度(n^3 2^n)

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e5 + 10, INF = 1e9 + 10, SS = 18;
    template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
    template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
    inline int read() {
    	char c = getchar(); int x = 0, f = 1;
    	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    	return x * f;
    }
    int N, M, Lim, a[SS][MAXN], f[1 << (SS)][SS], Mn[SS][MAXN], L[SS][MAXN];
    void Pre() {
    	for(int i = 1; i <= N; i++) {
    		for(int j = 1; j <= N; j++) {
    			Mn[i][j] = INF; L[i][j] = INF;
    			for(int k = 1; k <= M; k++) chmin(Mn[i][j], (i == j) ? a[i][k] : abs(a[i][k] - a[j][k]));
    			for(int k = 1; k < M; k++) chmin(L[i][j], abs(a[i][k] - a[j][k + 1]));
    		}
    	}
    }
    int DP(int bg) {
    	memset(f, -1, sizeof(f));
    	f[1 << (bg - 1)][bg] = INF;
    	for(int sta = 0; sta < Lim; sta++) {
    		for(int i = 1; i <= N; i++) {
    			if(f[sta][i] == -1) continue;
    			for(int j = 1; j <= N; j++) {
    				if(sta & (1 << (j - 1))) continue;
    				chmax(f[sta | (1 << (j - 1))][j], min(f[sta][i], Mn[i][j]));
    			}
    		}
    	}
    	int now = 0;
    	for(int i = 1; i <= N; i++) 
    		chmax(now, min(f[Lim][i], L[i][bg]));
    	return now;
    }
    int main() {
    	N = read(); M = read(); Lim = (1 << N) - 1;
    	for(int i = 1; i <= N; i++)
    		for(int j = 1; j <= M; j++) a[i][j] = read();
    	Pre();
    	int ans = 0;
    	for(int i = 1; i <= N; i++) 
    		chmax(ans, DP(i));
    	cout << ans;
    	return 0;
    }
    /*
    3 2
    85 6
    64 71
    1 83
    
    4 2
    9 9
    10 8
    5 3
    4 3
    */
    
  • 相关阅读:
    Yeelink 初探
    Macbook被格式化之后
    linux 代码分析工具 gprof
    Writing Clean Code 读后感
    0 bug 读后感
    STM32 控制红外线收发
    HomeKit 与老旧设备
    树莓派控制红外线收发
    苹果没法删除部分照片的问题
    route处理
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10252486.html
Copyright © 2020-2023  润新知