Codeforces Round #601 (Div. 1) A. Feeding Chicken(贪心+构造)
题目链接
大意:给你一个矩形,让你把矩形分成k个联通块,联通块内的R数的最大最小值相差最小,输出构造方案。
思路:蛇形填数的方式填即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e2 + 10;
#define fi first
#define se second
#define pb push_back
int t, n, m, k, s[N] ;char ans[N][N];
char a[N][N], c[N][N];
int main() {
ios::sync_with_stdio(false);
for (cin >> t; t; t--) {
cin >> n >> m >> k;
int rice = 0;
for (int i = 1; i <= n; i++)cin >> a[i] + 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] != 'R')continue;
rice++;
}
}
int ev = rice / k;
int da = rice % k;
for(int i=0;i<k;i++,da--){
s[i]=ev+(da>0);
}
int res=0;
auto turn = [] (int k){
if(k<=9)return k+'0';
k-=10;
if(k<26)return k+'a';
k-=26;
return k+'A';
};
for(int i=1;i<=n;i++){
if(i%2==1){
for(int j=1;j<=m;j++){
if(a[i][j]!='R'){
ans[i][j]=turn(res);
continue;
}
if(s[res]){
s[res]--;
}else{
res++;
s[res]--;
}
ans[i][j]=turn(res);
}
}else{
for(int j=m;j>=1;j--){
if(a[i][j]!='R'){
ans[i][j]=turn(res);
continue;
}
if(s[res]){
s[res]--;
}else{
res++;
s[res]--;
}
ans[i][j]=turn(res);
}
}
for(int j=1;j<=m;j++)cout<<ans[i][j];cout<<'\n';
}
}
return 0;
}