跳到正文

题解

我说 F > G 有没有懂的。

OneLastNOIP约 13 分钟
原文
文章信息与版本

文章信息

发布
2026年8月23日
收录
2026年8月24日
更新
2026年11月11日
篇幅
约 13 分钟
获赞
6

版本

  1. v2当前
  2. v1

题意

有一个 HH 行 WW 列的网格。每个格子上写有 +、-、# 三种字符之一。记从上往下第 ii 行、从左往右第 jj 列的格子为 (i,j)(i,j)。网格信息由 HH 个长度为 WW 的字符串 S1,S2,…,SHS_1, S_2, \dots, S_H 给出,其中 SiS_i 的第 jj 个字符表示格子 (i,j)(i,j) 上的字符。

你可以进行零次或多次以下操作:

  • 选择一个不是 # 的格子。然后,将“从该格子出发,仅通过向左、向右、向下三个方向移动到相邻格子,且不经过任何 # 格子所能到达的所有格子”全部变为 #。注意,选中的格子本身也算作可到达的格子。

操作结束后,请计算网格中 + 的数量减去 - 的数量,求这个值可能达到的最大值。

做法

这个题意看上去像状压,但是 W≤30W\le 30,但是可以发现一段连续的 + 和 - 是否被覆盖为 # 的情况是相同的,而且由于连续段之间必然有 # 隔开,所以连续段必然最多只有 1515 个。

现在可以状压,考虑设 dpi,Sdp_{i,S} 表示第 ii 行,被填充段的集合为 SS 的最大值,转移是显然的:

dpi,S=max⁡f(T)⊆Sdpi−1,T+∑j∉S∑k=ljrjaikdp_{i,S} = \max_{f(T)\subseteq S}dp_{i-1,T} +\sum_{j\notin S}\sum_{k=l_j}^{r_j}a_{ik}

其中 aika_{ik} 为原矩阵中该位置的值(+ 为 11,- 为 −1-1),f(T)f(T) 表示最大的集合满足 ∀j∈f(T)\forall j \in f(T),[lj,rj][l_j, r_j] 与 TT 中至少一条线段有交,相当于把 TT 转化为第 ii 行的集合。

然后直接做就完了。

C++
# 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
#endif
namespace 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 qword
typedef 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>=202300
typedef _Float16 $fw;typedef __bf16 $fbw;
#endif
typedef 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;}
原文