我说 F > G 有没有懂的。
OneLastNOIP · · 题解
题意
有一个 +、-、# 三种字符之一。记从上往下第
你可以进行零次或多次以下操作:
- 选择一个不是
#的格子。然后,将“从该格子出发,仅通过向左、向右、向下三个方向移动到相邻格子,且不经过任何#格子所能到达的所有格子”全部变为#。注意,选中的格子本身也算作可到达的格子。
操作结束后,请计算网格中 + 的数量减去 - 的数量,求这个值可能达到的最大值。
做法
这个题意看上去像状压,但是 + 和 - 是否被覆盖为 # 的情况是相同的,而且由于连续段之间必然有 # 隔开,所以连续段必然最多只有
现在可以状压,考虑设
其中 + 为 - 为
然后直接做就完了。
# 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;}