CF2194D Table Cut

思路

假设一共有 $tot$ 个 $1$ 块。
第一部分很简单:
注意到任何分配均可以成立,故最大值一定为 $\lfloor\frac{tot}{2}\rfloor\times\lceil\frac{tot}{2}\rceil$ 这样的构造。
考虑第二部分如何构造:
我们假设上半部分分 $tar=\lfloor\frac{tot}{2}\rfloor$ 个 $1$,考虑如何构造。
预处理第 $i$ 行的总和为 $cnt_i$,现在已经加入了 $now$ 个,若 $cnt_i+now<tar$,则直接加这一行。
若 $cnt_i+now\ge tar$,则从后往前枚举,若能加则加,不能加就说明现在已经够了。
这样的构造要如何转化为题目要求的输出呢?
首先若要加一整行,则直接输出 D,若要加上行末的 $k$ 格,则输出 $m-k$ 个 R,然后输出一个 D,再补上该行剩余的 R 即可。
最后输出剩下的 D

代码

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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=3e5+5;
int T,n,m,tot,cnt[N];
vector<int>a[N];
signed main(){
read(T);
while(T--){
read(n,m);tot=0;
rep(i,1,n){
a[i].clear();cnt[i]=0;
a[i].emplace_back(0);
rep(j,1,m){
int v;read(v);
tot+=v;cnt[i]+=v;
a[i].emplace_back(v);
}
}
int tar=tot/2,now=0,dcnt=n;
print(tar*(tot-tar));puts("");
rep(i,1,n){
if(cnt[i]+now<tar){
now+=cnt[i];
putchar('D');
dcnt--;
}
else{
int rcnt=m;
per(j,m,1){
if(now+a[i][j]<=tar){
now+=a[i][j];
rcnt--;
}
if(now==tar)break;
}
rep(j,1,rcnt){
putchar('R');
}
putchar('D');
dcnt--;
rep(j,rcnt+1,m){
putchar('R');
}
}
if(now==tar)break;
}
rep(i,1,dcnt)putchar('D');
puts("");
}
}