CodeForces 1255D Feeding Chicken(构造)_i++


CodeForces 1255D Feeding Chicken(构造)_i++_02


CodeForces 1255D Feeding Chicken(构造)_i++_03


CodeForces 1255D Feeding Chicken(构造)_#define_04


CodeForces 1255D Feeding Chicken(构造)_i++_05


题意: 现在有个n * m的格子,k只鸡。然后格子里如果输入的是R就代表有米,然后鸡要吃米。但是对于每只鸡分配的米必须要是联通的才能吃,问你怎么分配使得分到米最多的跟分到米,最少的差值最小,每只鸡最少分配1个米。这个差值肯定要么为0要么为1,统计下地图中R的个数,然后平均分给这k只鸡,每只鸡要么分R / k, 要么分到R / k + 1只。直接扫一遍地图,蛇形分配即可。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const int maxn = 3e5 + 5;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret*10 + ch - '0';
ch = getchar();
}
return ret*sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}

int qpow(int m, int k, int mod)
{
int res = 1, t = m;
while (k)
{
if (k&1)
res = res * t % mod;
t = t * t % mod;
k >>= 1;
}
return res;
}

ll gcd(ll a,ll b)
{
return b==0?a : gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
return a*b/gcd(a,b);
}

int i,j,k;
int n,m;
int r;
int cnt,res,temp;
char a[110][110];
bool flag;
bool ok;
vector<char>ans;
int main()
{
for(char i='0'; i<='9'; i++)
ans.pb(i);
for(char i='A'; i<='Z'; i++)
ans.pb(i);
for(char i='a'; i<='z'; i++)
ans.pb(i);
int t;
sd(t);
while(t--)
{
sddd(n,m,k);
cnt=0;
flag=0;
ok=0;
rep(i,1,n)
{
getchar();
rep(j,1,m)
{
sc(a[i][j]);
if(a[i][j]=='R')
{
cnt++;
//cout<<cnt<<endl;
}
}
}
//debug(cnt);
int x=cnt/k;
int y=cnt%k;
res=0;
int xx=x;
if(y)
{
xx++;
y--;
}
rep(i,1,n)
{
if(i&1)
{
rep(j,1,m)
{
if(a[i][j]=='R')
{
xx--;
cnt--;
}
a[i][j]=ans[res];
if(!cnt)
{
a[i][j]=ans[res];
continue;
}
if(!xx)
{
if(y)
{
xx=x+1;
y--;
}
else
xx=x;
res++;
}
}
}
else
{
per(j,1,m)
{
if(a[i][j]=='R')
{
xx--;
cnt--;
}
a[i][j]=ans[res];
if(!cnt)
{
a[i][j]=ans[res];
continue;
}
if(!xx)
{
if(y)
{
xx=x+1;
y--;
}
else
xx=x;
res++;
}
}
}
}
rep(i,1,n)
{
rep(j,1,m)
pc(a[i][j]);
printf("\n");
}
}
return 0;
}

///然后平均分给这k只鸡,每只鸡要么分R/k要么分到R/k+1只。