题意

题目链接

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
*/

cf1102F. Elongated Matrix(状压dp)的更多相关文章

  1. Codeforces 1102F Elongated Matrix 状压dp

    Elongated Matrix 预处理一下两两之间的最小值, 然后直接dp. #include<bits/stdc++.h> #define LL long long #define f ...

  2. Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

    F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...

  3. CF1102F Elongated Matrix

    题目地址:CF1102F Elongated Matrix 没想到Div.3里还有这么好的题 其实就是求Hamilton路径 预处理 \(d\) 数组: \(d1_{i,j}\) 表示第 \(i,j\ ...

  4. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  5. BZOJ 4000: [TJOI2015]棋盘( 状压dp + 矩阵快速幂 )

    状压dp, 然后转移都是一样的, 矩阵乘法+快速幂就行啦. O(logN*2^(3m)) ------------------------------------------------------- ...

  6. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. BZOJ_2734_[HNOI2012]集合选数_构造+状压DP

    BZOJ_2734_[HNOI2012]集合选数_构造+状压DP 题意:<集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x ...

  8. 【XSY2745】装饰地板 状压DP 特征多项式

    题目大意 你有\(s_1\)种\(1\times 2\)的地砖,\(s_2\)种\(2\times 1\)的地砖. 记铺满\(m\times n\)的地板的方案数为\(f(m,n)\). 给你\(m, ...

  9. 2018.09.28 hdu5434 Peace small elephant(状压dp+矩阵快速幂)

    传送门 看到n的范围的时候吓了一跳,然后发现可以矩阵快速幂优化. 我们用类似于状压dp的方法构造(1(1(1<<m)∗(1m)*(1m)∗(1<<m)m)m)大小的矩阵. 然后 ...

随机推荐

  1. Mac下如何安装配置Homebrew

    Last login: Mon Aug 7 13:57:29 on consolexiashenbindeMacBook-Pro:~ xiashenbin$ ruby -e "$(curl ...

  2. Java - 阅读与查找

    WebSites http://www.importnew.com/ https://www.java-tips.org/ http://www.javaworld.com/ http://www.p ...

  3. postgresql-distinct on理解

    PostgreSQL 的 distinct on 的理解 对于 select distinct on , 可以利用下面的例子来理解: create table a6(id integer, name ...

  4. 装饰器中的@functools.wraps的作用

    def login_required(view_func): @functools.wraps(view_func) def wrapper(*args, **kwargs): ...... retu ...

  5. Android使用bindService作为中间人对象开启服务

    Android使用bindService作为中间人对象开启服务 项目结构如下: MyService: package com.demo.secondservice; import android.ap ...

  6. 怎么样imageview实现铺满全屏

    <ImageView android:layout_width="match_parent" android:layout_height="match_parent ...

  7. Jquery初体验一

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. 微信小程序图片变形解决方法

    微信小程序的image标签中有个mode属性,使用aspectFill即可 注:image组件默认宽度300px.高度225px mode 有效值: mode 有 13 种模式,其中 4 种是缩放模式 ...

  9. 08 - JavaSE之IO流

    IO流 JAVA流式输入输出原理:可以想象成一根管道怼到文件上,另一端是我们程序,然后流的输入输出都是按照程序本身作为第一人称说明的.比如 input,对于我们程序来说就是有数据输入我们程序,outp ...

  10. Shell脚本 | 性能测试之启动时间

    安卓应用的性能测试,通常包括六个指标:启动时间.内存.CPU.耗电量.流量.流畅度. 除了耗电量,其他五个指标的数据在我们团队中已经可以通过运行脚本的方式获取到. 今天给大家分享下启动时间的脚本吧- ...