CF2225C Red-Black Pairs

思路

只有两行,所以是简单 DP。
考虑只能通过竖着的一对或横着且左右端点相同的两对矩形组成最后的大矩形,否则会产生一个一格的缺口,一定填不满。
那么设 $f_i$ 表示要填第 $i$ 列时的最小代价,算出本次转移代价再向 $f_{i+1}$ 和 $f_{i+2}$ 转移即可。
最后取 $f_{n+1}$ 为答案(要填 $n+1$,已经填完 $n$)。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<bits/stdc++.h>
#define fir first
#define sec second
#define int long long
#define pii pair<int,int>
#define fep(i,s,e) for(int i=s;i<e;i++)
#define pef(i,s,e) for(int i=s;i>e;i--)
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define per(i,s,e) for(int i=s;i>=e;i--)
namespace FastIO{
template<typename T>inline void read(T &x){
x=0;int f=1;char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);x*=f;
}
template<typename T,typename...Args>
inline void read(T &x,Args&...args){
read(x);
read(args...);
}
template<typename T>void print(T x){
if(x<0)x=-x,putchar('-');
if(x>9)print(x/10);
putchar((x%10)^48);
}
}
using namespace std;
using namespace FastIO;
const int N=2e5+47;
int T,n,a[3][N],f[N];
signed main(){
read(T);
while(T--){
read(n);
rep(i,1,2){
rep(j,1,n){
char c;cin>>c;
if(c=='R')a[i][j]=1;
else a[i][j]=0;
}
}
rep(i,2,n+1)f[i]=n*3;
rep(i,1,n){
if(a[1][i]==a[2][i])f[i+1]=min(f[i+1],f[i]);
if(a[1][i]!=a[2][i])f[i+1]=min(f[i+1],f[i]+1);
if(a[1][i]==a[1][i+1]&&a[2][i]==a[2][i+1])f[i+2]=min(f[i+2],f[i]);
if(a[1][i]!=a[1][i+1]&&a[2][i]==a[2][i+1])f[i+2]=min(f[i+2],f[i]+1);
if(a[1][i]==a[1][i+1]&&a[2][i]!=a[2][i+1])f[i+2]=min(f[i+2],f[i]+1);
if(a[1][i]!=a[1][i+1]&&a[2][i]!=a[2][i+1])f[i+2]=min(f[i+2],f[i]+2);
}
print(f[n+1]);
puts("");
}
}