版本比较
我说 F > G 有没有懂的。
正文
增加 159 行删除 0 行
19 行未改动202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118219 行未改动
其中 $a_{ik}$ 为原矩阵中该位置的值(`+` 为 $1$,`-` 为 $-1$),$f(T)$ 表示最大的集合满足 $\forall j \in f(T)$,$[l_j, r_j]$ 与 $T$ 中至少一条线段有交,相当于把 $T$ 转化为第 $i$ 行的集合。
然后直接做就完了。
```cpp# include <bits/extc++.h># define rep(i, a, b) for(int i = (a);i <= (b);i ++)# define lop(i, a, b) for(int i = (a);i < (b);i ++)# define dwn(i, a, b) for(int i = (a);i >= (b);i --)# define int64 long long# define ___main signed main()# define f64 double# define f32 float# define f128 long double# define int128 __int128# define i128 ((__int128)1)# define int32 signed# define un unsigned# define int16 short# define int8 char# define ret return# define inl inline# define reg register# define fn void# define elif else if# define pb push_back# define pp pop_back# define CASES() int T;cin >> T;while(T--)# define FaILurEg(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)#if defined(_MSC_VER)# define FORCE_INLINE __forceinline#else# define FORCE_INLINE __attribute__((always_inline)) inline#endifnamespace packin {using std::cout;using std::cin;template<typename T>FORCE_INLINE fn read(T &x) {cin >> x;}template<typename T>FORCE_INLINE fn read(T *a, int siz) {rep(i, 1, siz) cin >> a[i];}template<typename T, typename... Args>FORCE_INLINE void read(T &x, Args&... args) {read(x);read(args...);}inline void print_impl(const char* s) {while (*s) {cout << *s++;}}template<typename T, typename... Args>inline void print_impl(const char* s, const T& x, const Args&... args) {while (*s) {if (s[0] == '{' && s[1] == '}') {cout << x;print_impl(s + 2, args...);return;}cout << *s++;}}template<typename... Args>inline void print(const char* s, const Args&... args) {print_impl(s, args...);}inline void error_impl(const char*s){while(*s)std::cerr<<*s++;}template<class T,class...A>inline void error_impl(const char*s,const T&x,const A&...a){while(*s){if(s[0]=='{'&&s[1]=='}'){std::cerr<<x;error_impl(s+2,a...);return;}std::cerr<<*s++;}}template<class...A>inline void error(const char*s,const A&...a){error_impl(s,a...);}void IOS() {std::ios::sync_with_stdio(0);std::cin.tie(0);}}template<typename T>FORCE_INLINE T min(T a, T b) {ret (a < b)?a:b;}template<typename T>FORCE_INLINE T max(T a, T b) {ret (a > b)?a:b;}using packin::read;using packin::print;using packin::error;using std::pair;using std::vector;using packin::IOS;#define DEFp(a, A, b, B) typedef pair<a, b> p##A##B;#define DEFM(a, A, b, B) typedef std::map<a, b> $m##A##B;#define DEFMu(a, A, b, B) typedef std::unordered_map<a, b> $um##A##B;#define DEFG(a, A, b, B) typedef __gnu_pbds::gp_hash_table<a, b> $g##A##B;#define FOR_EACH_LEFT(m) \ m(int, i) \ m(long long, l) \ m(double, d) \ m(long double, ld) #define FOR_EACH_RIGHT(m, T1, S1) \ m(T1, S1, int, i) \ m(T1, S1, long long, l) \ m(T1, S1, double, d) \ m(T1, S1, long double, ld)
// 笛卡尔积展开#define EXPAND_LEFT(T1, S1) FOR_EACH_RIGHT(DEFp, T1, S1)
// 一句触发FOR_EACH_LEFT(EXPAND_LEFT)#undef EXPAND_LEFT
#define EXPAND_LEFT(T1, S1) FOR_EACH_RIGHT(DEFM, T1, S1)FOR_EACH_LEFT(EXPAND_LEFT)#undef EXPAND_LEFT
#define EXPAND_LEFT(T1, S1) FOR_EACH_RIGHT(DEFMu, T1, S1)FOR_EACH_LEFT(EXPAND_LEFT)#undef EXPAND_LEFT
#define EXPAND_LEFT(T1, S1) FOR_EACH_RIGHT(DEFG, T1, S1)FOR_EACH_LEFT(EXPAND_LEFT)#undef EXPAND_LEFT
#define DEFX(TYPE, TY, X, x) typedef TYPE<X> TY##x;
#define MAKELIST(m, TYPE, TY) m(TYPE, TY, int, i)m(TYPE, TY, long long, l)m(TYPE, TY, double, d)m(TYPE, TY, long double, ld)
MAKELIST(DEFX, vector, $v);MAKELIST(DEFX, std::basic_string, $bs);MAKELIST(DEFX, std::set, $S);//w: word d Dword q qwordtypedef short $w;typedef int $d;typedef int64 $q;typedef int128 $o;typedef un short u$w;typedef un int u$d;typedef un int64 u$q;typedef un int128 u$o;typedef float $fd;typedef double $fq;typedef long double $fqu;/*Quint* */typedef __float128 $fo;#if __cplusplus>=202300typedef _Float16 $fw;typedef __bf16 $fbw;#endiftypedef std::string _Str;int h, w;_Str mat[35];$d dp[35][65536], tmp[65536];
vector<pii>v[35];
___main { read(h, w); rep(i, 1, h) read(mat[i]), mat[i] = " " + mat[i]; rep(i, 1, h) { // cut '+''-' with '#' rep(j, 1, w) if(mat[i][j] != '#') { $d r = j; while(r < w && mat[i][r + 1] != '#') r++; v[i].pb({j, r}); j = r; } } memset(dp, -0x3f, sizeof dp); v[0].pb({1, w}); dp[0][0] = 0; rep(i, 0, h - 1) { memset(tmp, -0x3f, sizeof tmp); rep(j, 0, (1 << (v[i].size())) - 1) { // convert line j to line j + 1 $d mask = 0; $d t = j; while(t) { $d x = std::__lg(t & -t); rep(k, v[i][x].first, v[i][x].second) mask |= (1 << (k - 1)); t -= (t & -t); }
$d J = 0; rep(k, 0, ($d)v[i + 1].size() - 1) { $d f = 0; rep(l, v[i + 1][k].first, v[i + 1][k].second) f |= ((mask >> (l - 1)) & 1); J |= (f << k); } tmp[J] = max(tmp[J], dp[i][j]); } rep(j, 0, (1 << v[i + 1].size()) - 1) { rep(k, 0, ($d)v[i + 1].size() - 1) if((j & (1 << k))) tmp[j] = max(tmp[j], tmp[j ^ (1 << k)]); dp[i + 1][j] = tmp[j]; rep(k, 0, ($d)v[i + 1].size() - 1) if(!(j & (1 << k))) rep(l, v[i + 1][k].first, v[i + 1][k].second) dp[i + 1][j] += (mat[i + 1][l] == '+' ? 1 : -1); } } //print("{}\n", dp[1][0]); $d Ans = -0x3f3f3f3f; rep(j, 0, (1 << v[h].size()) - 1) Ans = max(Ans, dp[h][j]); print("{}\n", Ans); ret 0;}```