题解 AT4890 【[ABC136D] Gathering Children】
Discription
给定一个长为
Solution
模拟+找规律。
首先不要被
我们可以把字符串看成多个由
之后便是找规律看多次移动后点的情况。可以用竖式在Excel里模拟下每轮情况。不难发现,当我们把区间内的点都聚集在区间内
非最优解法。
Code
#include<bits/stdc++.h>
using namespace std;
string s;
int n,a[100001],res[100001];
bool ifcr=1,ifcl=0;
int cr,cl,nl;
int main()
{
cin>>s;
n=s.length();
s+='O';
//统计区间内L R个数
for(int i=0;i<n+1;i++)
{
if(ifcr)
{
if(s[i]=='R') cr++;
else
{
a[i-1]=cr;
cr=0;
ifcr=0;ifcl=1;
nl=i;
}
}
if(ifcl)
{
if(s[i]=='L') cl++;
else
{
a[nl]=cl;
cl=0;
ifcr=1;ifcl=0;
cr++;
}
}
}
//计算交换情况
for(int i=0;i<n;i++)
{
if(a[i]!=0)
{
if((a[i]+a[i+1])%2==0)
{
res[i]=(a[i]+a[i+1])/2;
res[i+1]=res[i];
}
else
{
res[i]=floor((a[i]+a[i+1])/2);
res[i+1]=res[i];
if(a[i]%2==0) res[i+1]++;
else res[i]++;
}
i++;
}
}
for(int i=0;i<n;i++) cout<<res[i]<<" ";
return 0;
}