题解:CF1394C Boboniu and String
[CF1394C] Boboniu and String
为什么题解都带
为了方便起见,下文将
那么将
考虑设
#include<bits/stdc++.h>
#define ll long long
#define pn putchar('\n')
#define mset(a,x) memset(a,x,sizeof a)
#define mcpy(a,b) memcpy(a,b,sizeof a)
#define all(a) a.begin(),a.end()
#define fls() fflush(stdout)
#define maxn 500005
using namespace std;
int re()
{
int x=0;
bool t=1;
char ch=getchar();
while(ch>'9'||ch<'0')
t=ch=='-'?0:t,ch=getchar();
while(ch>='0'&&ch<='9')
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return t?x:-x;
}
void gx(int &x,int y)
{
if(y>x)
x=y;
}
void gn(int &x,int y)
{
if(y<x)
x=y;
}
int n;
char a[maxn];
signed main()
{
n=re();
int x1=1e9,x2=0,y1=1e9,y2=0,s1=1e9,s2=-1e9;
while(n--)
{
scanf("%s",a+1);
int m=strlen(a+1);
int c1=0,c2=0;
for(int i=1;i<=m;i++)
{
if(a[i]=='B')
c1++;
else
c2++;
}
gn(x1,c1),gx(x2,c1);
gn(y1,c2),gx(y2,c2);
gn(s1,c1-c2),gx(s2,c1-c2);
}
int ans=1e9,x,y;
for(int i=x1;i<=x2;i++)
{
int l=min(y1,i-s2),r=max(y2,i-s1);
int j=l+r>>1;
int t=max({i-x1,x2-i,j-l,r-j});
if(t<=ans)
{
ans=t;
x=i,y=j;
}
}
printf("%d\n",ans);
while(x--)
putchar('B');
while(y--)
putchar('N');
pn;
return 0;
}