题目描述
你有一个 H × W H\times W H × W 的网格,初始时网格中的格子都是白色的。
给你一个长度为 H + W − 2 H+W-2 H + W − 2 、由 D、R 和 ? 组成的字符串 S S S ,你要执行若干次以下操作:
走一条从 ( 1 , 1 ) (1,1) ( 1 , 1 ) 到 ( H , W ) (H,W) ( H , W ) 的路径,每一步只能选择向下或向右走一格,若 S i = D S_i= \texttt{D} S i = D 则第 i i i 步只能选择往下走,若 S i = R S_i=\texttt{R} S i = R 则第 i i i 步只能往右走。
把路径上经过的所有格子,包括开头结尾,全部染成黑色。
求你最多能够把多少个格子染成黑色。
数据范围
保证 1 ≤ T ≤ 2 × 10 5 , 2 ≤ H , W ≤ 2 × 10 5 1\le T\le 2\times 10^5,2\le H,W\le 2\times 10^5 1 ≤ T ≤ 2 × 1 0 5 , 2 ≤ H , W ≤ 2 × 1 0 5 ,通过 S S S 能够构造出的合法路径至少有一条。
保证单个测试点中 ∑ ( H + W ) ≤ 4 × 10 5 \sum(H+W)\le 4\times 10^5 ∑ ( H + W ) ≤ 4 × 1 0 5 。
题目分析
先想办法判断点 ( x , y ) (x,y) ( x , y ) 是否可以被走到。发现这等价于可以从 ( 1 , 1 ) (1,1) ( 1 , 1 ) 走到 ( x , y ) (x,y) ( x , y ) ,再从 ( x , y ) (x,y) ( x , y ) 走到 ( H , W ) (H,W) ( H , W ) 。要从 ( 1 , 1 ) (1,1) ( 1 , 1 ) 走到 ( x , y ) (x,y) ( x , y ) ,需要一个长度为x + y − 2 x+y-2 x + y − 2 的字符串,记为k k k ,不难发现,前 k k k 步会出现 ( x − 1 ) (x-1) ( x − 1 ) 个 D \texttt{D} D 和 ( y − 1 ) (y-1) ( y − 1 ) 个 R \texttt{R} R 。那我们记 d i d_i d i 为前 i i i 个字符中 D \texttt{D} D 的个数,r i r_i r i 为前 i i i 个字符中 R \texttt{R} R 的个数,s i s_i s i 为前 i i i 个字符中 ? ? ? 的个数。既然前 k k k 步会出现 ( x − 1 ) (x-1) ( x − 1 ) 个 D \texttt{D} D 和 ( y − 1 ) (y-1) ( y − 1 ) 个 R \texttt{R} R ,那么可以知道:
d i ≤ x − 1 d_i\le x-1 d i ≤ x − 1 ,
r i ≤ y − 1 r_i\le y-1 r i ≤ y − 1
那么还剩下 x − 1 − d i x-1-d_i x − 1 − d i 个 D \texttt{D} D ,y − 1 − r i y-1-r_i y − 1 − r i 个 R \texttt{R} R 要用 ? \texttt{?} ? 去补。我们令 n n n 和 m m m 为还要几个 D \texttt{D} D 和还要几个R \texttt{R} R ,可以得出n = H − 1 − d H + W − 2 n=H-1-d_{H+W-2} n = H − 1 − d H + W − 2 ,m = W − 1 − r H + W − 2 m=W-1-r_{H+W-2} m = W − 1 − r H + W − 2 。那么,我们想,前 i i i 个中,可以变成 D \texttt{D} D 的 ? \texttt{?} ? 有 min ( s i , n ) \min(s_i,n) min ( s i , n ) ,可以变成 R \texttt{R} R 的 ? \texttt{?} ? 有 min ( s i , m ) \min(s_i,m) min ( s i , m ) 。
因此要调整使得可以走到 ( x , y ) (x,y) ( x , y ) 需要满足 x − 1 − d i ≤ min ( s i , n ) x−1−d_i \le \min(s_i,n) x − 1 − d i ≤ min ( s i , n ) ,y − 1 − r i ≤ min ( s i , m ) y−1−r_i \le \min(s_i ,m) y − 1 − r i ≤ min ( s i , m ) 。自己去总结一下这些条件,汇总一下,这里暂时不展示(太懒了)。然后,我们可以进行合并,得到以下条件:
d i + 1 ≤ x ≤ min ( s i , n ) + d i + 1 d_i+1 \le x \le \min(s_i,n)+d_i+1 d i + 1 ≤ x ≤ min ( s i , n ) + d i + 1
r i + 1 ≤ y ≤ min ( s i , m ) + r i + 1 r_i+1 \le y \le \min(s_i,m)+r_i+1 r i + 1 ≤ y ≤ min ( s i , m ) + r i + 1
OK啊,是不是觉得这个式子挺简洁的,可以写代码了。但是,注意,有两个位置数,时间复杂度是 O ( n m ) \mathcal{O}(nm) O ( nm ) ,能过吗?显然不能,两个未知数肯定不行,那我们得简化一下,简化到只要枚举一个对象。
我们可以记一个 i i i ,满足 x + y − 2 = i x+y-2=i x + y − 2 = i ,我们枚举 i i i ,那么 y = i − x + 2 y=i-x+2 y = i − x + 2 :
d i + 1 ≤ x ≤ min ( s i , n ) + d i + 1 d_i+1 \le x \le \min(s_i ,n)+d_i+1 d i + 1 ≤ x ≤ min ( s i , n ) + d i + 1 ,
r i + 1 ≤ k − x + 2 ≤ min ( s i , m ) + r i + 1 r_i+1 \le k−x+2 \le \min(s_i,m)+r_i+1 r i + 1 ≤ k − x + 2 ≤ min ( s i , m ) + r i + 1
然后,只有x x x 一个未知数,我们求它的范围:
max ( d i + 1 , k − min ( s i , m ) − r i + 1 ) ≤ x ≤ min ( min ( s i , n ) + d i + 1 , k − r i + 1 ) \max(d_i+1,k−\min(s_i,m)−r_i+1) \le x \le \min(\min(s_i,n)+d_i+1,k−r_i+1) max ( d i + 1 , k − min ( s i , m ) − r i + 1 ) ≤ x ≤ min ( min ( s i , n ) + d i + 1 , k − r i + 1 )
那么就好了(唉,推式子把孩子推死了),代码上:
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define LL long long
const int maxn= 2 e 5 + 5 ;
int t,h,w,d[maxn* 2 ],r[maxn* 2 ],s[maxn* 2 ];
string str;
int main (){
cin>> t;
while (t-- ){
cin>> h>> w>> str;
str= " " + str;
for (int i= 1 ;i<= h+ w- 2 ;i++ )
d[i]= d[i- 1 ]+ (str[i]== 'D' ),
r[i]= r[i- 1 ]+ (str[i]== 'R' ),
s[i]= s[i- 1 ]+ (str[i]== '?' );
int n= h- 1 - d[h+ w- 2 ];
int m= w- 1 - r[h+ w- 2 ];
LL ans= 0 ;
for (int i= 0 ;i<= h+ w- 2 ;i++ ){
int L= max (d[i]+ 1 ,i- min (s[i],m)- r[i]+ 1 );
int R= min (min (s[i],n)+ d[i]+ 1 ,i- r[i]+ 1 );
ans+= R- L+ 1 ;
}
cout<< ans<< endl;
}
return 0 ;
}