Codeforces 1102F Elongated Matrix 状压dp
预处理一下两两之间的最小值, 然后直接dp。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e4 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, m;
int Mat[][N];
int go[][];
int go2[][];
int dp[ << ][]; int main() {
scanf("%d%d", &n, &m);
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
scanf("%d", &Mat[i][j]);
int ans = ;
if(n == ) {
ans = inf;
for(int i = ; i < m - ; i++) ans = min(ans, abs(Mat[][i] - Mat[][i + ]));
printf("%d\n", ans);
return ;
}
for(int i = ; i < n; i++) {
for(int j = ; j < n; j++) {
if(i == j) continue;
go[i][j] = go2[i][j] = inf;
for(int k = ; k < m; k++)
go[i][j] = min(go[i][j], abs(Mat[i][k] - Mat[j][k]));
for(int k = ; k < m - ; k++) {
go2[i][j] = min(go2[i][j], abs(Mat[i][k] - Mat[j][k + ]));
}
}
}
for(int start = ; start < n; start++) {
memset(dp, -, sizeof(dp));
dp[ << start][start] = inf;
for(int S = ; S < ( << n); S++) {
for(int i = ; i < n; i++) {
if(dp[S][i] < ) continue;
for(int j = ; j < n; j++) {
if(S >> j & ) continue;
dp[S|(<<j)][j] = max(dp[S|(<<j)][j], min(go[i][j], dp[S][i]));
}
}
}
for(int i = ; i < n; i++)
ans = max(ans, min(go2[i][start], dp[(<<n)-][i]));
}
printf("%d\n", ans);
return ;
} /*
*/
Codeforces 1102F Elongated Matrix 状压dp的更多相关文章
- cf1102F. Elongated Matrix(状压dp)
题意 题目链接 Sol \(n \leqslant 16\)可以想到状压 我们可以预处理出任意两行之间每列的最小值以及相邻两列的最小值 然后枚举一个起点,\(f[sta][i]\)表示走过了\(sta ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- Codeforces 917C - Pollywog(状压 dp+矩阵优化)
UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...
- Codeforces 79D - Password(状压 dp+差分转化)
Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ...
- Codeforces 544E Remembering Strings 状压dp
题目链接 题意: 给定n个长度均为m的字符串 以下n行给出字符串 以下n*m的矩阵表示把相应的字母改动成其它字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在 ...
- codeforces 21D. Traveling Graph 状压dp
题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...
- Codeforces 895C - Square Subsets 状压DP
题意: 给了n个数,要求有几个子集使子集中元素的和为一个数的平方. 题解: 因为每个数都可以分解为质数的乘积,所有的数都小于70,所以在小于70的数中一共只有19个质数.可以使用状压DP,每一位上0表 ...
- CodeForces 327E Axis Walking(状压DP+卡常技巧)
Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub ...
- Codeforces ----- Kefa and Dishes [状压dp]
题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...
随机推荐
- VUE2.0 饿了吗视频学习笔记(一):VUE示例data.json
https://gitee.com/1981633/vue_study.git 源码下载地址,随笔记动态更新中有的同学找不到data.json,以下是data.json内容 { "selle ...
- Spring第一个helloWorld
Spring 简介: 轻量级:Spring是非侵入性的-基于Spring开发的应用中的对象可以不依赖于Spring的API 依赖注入(DI—dependdency injection.IOC) 面向切 ...
- Tessaract 源码分析(转)
源码分析 Page Layout 分析步骤 二值化算法: OTSU调用栈:main[api/tesseractmain.cpp] -> TessBaseAPI::ProcessPages[api ...
- VC++中LogFont设置字体(转)
LOGFONT是Windows内部字体的逻辑结构,主要用于设置字体格式,其定义如下:typedef struct tagLOGFONTA{LONG lfHeight;LONG lfWidth;LONG ...
- pyqt5-多线程QThread类
要实现多线程,我们要先继承QThread类并重新实现其中的run()函数,也就是说把耗时的操作放入run()函数中 import sys from PyQt5.QtCore import Qt, QT ...
- luogu P2516 [HAOI2010]最长公共子序列
传送门 首先那个\(O(n^2)\)的dp都会吧,不会自己找博客或者问别人,或是去做模板题(误) 对以下内容不理解的,强势推荐flash的博客 我们除了原来记录最长上升子序列的\(f_{i,j}\), ...
- python 线程 event
全局定义了一个“Flag”,如果“Flag”值为 False,那么当程序执行 event.wait 方法时就会阻塞,如果“Flag”值为True,那么执行event.wait 方法时便不再阻塞. cl ...
- Android NetworkInterface 的 name
user@android:/$ ls /sys/class/net/ dummy0 lo p2p0 rev_rmnet0 rev_rmnet1 rev_rmnet2 rev_rmnet3 rmnet0 ...
- swift 计算100000以内的 回文数
... { var rep = var aa = a repeat{ rep = rep * + aa % aa = aa / }) if(rep == a) { print("\(a)是回 ...
- ubuntu 14.04 软件中心闪退解决方案
法一: gksudo gedit /usr/share/software-center/softwarecenter/ui/gtk3/views/lobbyview.py 注释下面这句话(注释使用#号 ...