Elongated Matrix

预处理一下两两之间的最小值, 然后直接dp。

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define fi first
  4. #define se second
  5. #define mk make_pair
  6. #define PLL pair<LL, LL>
  7. #define PLI pair<LL, int>
  8. #define PII pair<int, int>
  9. #define SZ(x) ((int)x.size())
  10. #define ull unsigned long long
  11.  
  12. using namespace std;
  13.  
  14. const int N = 1e4 + ;
  15. const int inf = 0x3f3f3f3f;
  16. const LL INF = 0x3f3f3f3f3f3f3f3f;
  17. const int mod = 1e9 + ;
  18. const double eps = 1e-;
  19. const double PI = acos(-);
  20.  
  21. int n, m;
  22. int Mat[][N];
  23. int go[][];
  24. int go2[][];
  25. int dp[ << ][];
  26.  
  27. int main() {
  28. scanf("%d%d", &n, &m);
  29. for(int i = ; i < n; i++)
  30. for(int j = ; j < m; j++)
  31. scanf("%d", &Mat[i][j]);
  32. int ans = ;
  33. if(n == ) {
  34. ans = inf;
  35. for(int i = ; i < m - ; i++) ans = min(ans, abs(Mat[][i] - Mat[][i + ]));
  36. printf("%d\n", ans);
  37. return ;
  38. }
  39. for(int i = ; i < n; i++) {
  40. for(int j = ; j < n; j++) {
  41. if(i == j) continue;
  42. go[i][j] = go2[i][j] = inf;
  43. for(int k = ; k < m; k++)
  44. go[i][j] = min(go[i][j], abs(Mat[i][k] - Mat[j][k]));
  45. for(int k = ; k < m - ; k++) {
  46. go2[i][j] = min(go2[i][j], abs(Mat[i][k] - Mat[j][k + ]));
  47. }
  48. }
  49. }
  50. for(int start = ; start < n; start++) {
  51. memset(dp, -, sizeof(dp));
  52. dp[ << start][start] = inf;
  53. for(int S = ; S < ( << n); S++) {
  54. for(int i = ; i < n; i++) {
  55. if(dp[S][i] < ) continue;
  56. for(int j = ; j < n; j++) {
  57. if(S >> j & ) continue;
  58. dp[S|(<<j)][j] = max(dp[S|(<<j)][j], min(go[i][j], dp[S][i]));
  59. }
  60. }
  61. }
  62. for(int i = ; i < n; i++)
  63. ans = max(ans, min(go2[i][start], dp[(<<n)-][i]));
  64. }
  65. printf("%d\n", ans);
  66. return ;
  67. }
  68.  
  69. /*
  70. */

Codeforces 1102F Elongated Matrix 状压dp的更多相关文章

  1. cf1102F. Elongated Matrix(状压dp)

    题意 题目链接 Sol \(n \leqslant 16\)可以想到状压 我们可以预处理出任意两行之间每列的最小值以及相邻两列的最小值 然后枚举一个起点,\(f[sta][i]\)表示走过了\(sta ...

  2. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  3. Codeforces 917C - Pollywog(状压 dp+矩阵优化)

    UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...

  4. Codeforces 79D - Password(状压 dp+差分转化)

    Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ...

  5. Codeforces 544E Remembering Strings 状压dp

    题目链接 题意: 给定n个长度均为m的字符串 以下n行给出字符串 以下n*m的矩阵表示把相应的字母改动成其它字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在 ...

  6. codeforces 21D. Traveling Graph 状压dp

    题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...

  7. Codeforces 895C - Square Subsets 状压DP

    题意: 给了n个数,要求有几个子集使子集中元素的和为一个数的平方. 题解: 因为每个数都可以分解为质数的乘积,所有的数都小于70,所以在小于70的数中一共只有19个质数.可以使用状压DP,每一位上0表 ...

  8. CodeForces 327E Axis Walking(状压DP+卡常技巧)

    Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub ...

  9. Codeforces ----- Kefa and Dishes [状压dp]

    题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...

随机推荐

  1. SQL Server进阶(一)T-SQL查询和编程的背景

  2. log4j2 的使用

    log4j2 是 log4j 的升级,更为方便,更为强大. log4j2.xml 的配置以及 log4j2的依赖包使用log4j2 并没有其他的依赖包,只是在使用log4j的情况下,需要别的进行桥接 ...

  3. WF控制台工作流(1)

    简单使用WF工作流示例: using System; using System.Linq; using System.Activities; using System.Activities.State ...

  4. jquery选择器最后一个,倒数第二个元素

    <div> <p>1</p> <p>2</p> <p>3</p> <p>4</p> < ...

  5. Java读取Excel转换成JSON字符串进而转换成Java对象

    Jar包

  6. ROS 错误之 [rospack] Error: package 'beginner_tutorials' not found

    ubuntu 下面情况处理 $ cd $gedit .bashrc 再后面加入两行 source /opt/ros/indigo/setup.bash source /home/lv/catkin_w ...

  7. 【API】检查进程是否存在 - CreateToolhelp32Snapshot

    1 学习目标 今天静态逆向mydocument病毒时,看到病毒代码为了防止自身被调试会先检测杀毒软件和调试工具的进程是否存在.如果没有杀毒软件则释放真正的病毒文件,提前熟悉一下枚举进程的反汇编代码. ...

  8. Visual Studio 2017中的快捷键

    Ctrl+Tab: 快速切换活动文件

  9. C++经典面试题(最全,面中率最高)

    C++经典面试题(最全,面中率最高) 1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new对应free只会释放内存,new调用构造函数.malloc与fre ...

  10. Vue.js——component(组件)

    概念: 组件(Component)是自定义元素. 作用: 可以扩展HTML元素,封装可重用的代码. <div id="myView"> <!-- 把学生的数据循环 ...