先提前处理求出u,d,l,r的前缀数组,然后 二分修改区间的长度即可
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int n,x7,y7,x2,y2,preu[maxn],pred[maxn],prel[maxn],prer[maxn];
bool check(int len)
{
int tmp,st;
for(int ed=len;ed<=n;ed++)
{
st=ed-len+1;
x2=prer[st-1]-prel[st-1]+prer[n]-prer[ed]-(prel[n]-prel[ed]);
y2=preu[st-1]-pred[st-1]+preu[n]-preu[ed]-(pred[n]-pred[ed]);
tmp=abs(x7-x2)+abs(y7-y2);
if(len>=tmp&&(len-tmp)%2==0)
return true;
}
return false;
}
int main()
{
int minn=-1,low,high,mid;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
char tmp;
scanf(" %c",&tmp);
prer[i]=prer[i-1];
prel[i]=prel[i-1];
preu[i]=preu[i-1];
pred[i]=pred[i-1];
if(tmp=='R')
prer[i]+=1;
else
if(tmp=='L')
prel[i]+=1;
else
if(tmp=='U')
preu[i]+=1;
else
pred[i]+=1;
}
scanf("%d %d",&x7,&y7);
if((prer[n]-prel[n]==x7)&&(preu[n]-pred[n]==y7))
{
printf("0
");
return 0;
}
low=1,high=n;
while(low<=high)
{
mid=(low+high)/2;
if(check(mid))
{
minn=mid;
high=mid-1;
}
else
low=mid+1;
}
printf("%d
",minn);
return 0;
}