codefores 1073C (二分 + 前缀和)

C. Vasya and Robot

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output


Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell (0,0). Robot can perform the following four kinds of operations:

U — move from (x,y) to (x,y+1);
D — move from (x,y) to (x,y−1);
L — move from (x,y) to (x−1,y);
R — move from (x,y) to (x+1,y).
Vasya also has got a sequence of n operations. Vasya wants to modify this sequence so after performing it the robot will end up in (x,y).

Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: maxID−minID+1, where maxID is the maximum index of a changed operation, and minID is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices 2, 5 and 7 are changed, so the length of changed subsegment is 7−2+1=6. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is 1.

If there are no changes, then the length of changed subsegment is 0. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.

Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from (0,0) to (x,y), or tell him that it's impossible.

Input
The first line contains one integer number n (1≤n≤2⋅105) — the number of operations.

The second line contains the sequence of operations — a string of n characters. Each character is either U, D, L or R.

The third line contains two integers x,y (−109≤x,y≤109) — the coordinates of the cell where the robot should end its path.

Output
Print one integer — the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from (0,0) to (x,y). If this change is impossible, print −1.

Examples
input
5
RURUU
-2 3
output
3
input
4
RULR
1 1
output
0
input
3
UUU
100 100
output
-1
Note
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is 3−1+1=3.

In the second example the given sequence already leads the robot to (x,y), so the length of the changed subsegment is 0.

In the third example the robot can't end his path in the cell (x,y).

 

题意:给定一个字符串 只包含 LRUD,分别代表向  左右上下 移动。然后给定一个坐标 x,y. 表示你将要到达的终点。现在需要改变字符串 使能够到达  x,y . 假设改变的最小的一个字符的下标为 minid , 最大的一个字符下标为 maxid. 题目要求 maxid - minid + 1 最小。 输出这个最小值。

思路:说真的  想不到二分。最后是看的题解思路。   大概是  用两个前缀和分别存 x 和 y.  最后通过二分枚举改变的长度,check这个长度是否能够到达终点即可。

具体的check 函数:假设枚举的长度为 mid, 起点为 i,终点就为 i + mid  - 1, 通过前缀和算出 这个区间以外 对x 和 y 的贡献。然后你需要知道,这个 [i,i + mid - 1] 这个区间其实可以是任意改变的,因为只需要这个 mid 最小,并没有限制 改变的字符的数量最小。

AC代码:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define INT(t) int t; scanf("%d",&t)
#define LLI(t) LL t; scanf("%I64d",&t)

using namespace std;

const int maxn = 2e5 + 10;
char str[maxn];
int sx[maxn];
int sy[maxn];
int x,y,n;;

bool check(int mid){
    for(int i = 1;i + mid - 1 <= n;i ++){
        int fx = sx[i - 1] + sx[n] - sx[i + mid - 1];
        int fy = sy[i - 1] + sy[n] - sy[i + mid - 1];
        int nedx = x - fx, nedy = y - fy;
        if(abs(nedx) + abs(nedy) <= mid && (mid - abs(nedx) - abs(nedy)) % 2 == 0)
            /// abs(nedx) + abs(nedy) <= mid 保证需要改变的字符数量足够
            /// % 2 == 0 是保证 在还有多余字符的情况下,保证 能够来回 以 最后到达终点
            return true;
    }
    return false;
}

int main()
{
    while(~scanf("%d",&n)){
        scanf("%s",str + 1);
        scanf("%d%d",&x,&y);
        for(int i = 1;i <= n;i ++){
            sx[i] = sx[i - 1] + (str[i] == 'L' ? -1 : (str[i] == 'R' ? 1 : 0));
            sy[i] = sy[i - 1] + (str[i] == 'D' ? -1 : (str[i] == 'U' ? 1 : 0));
        }
        int l = 0,r = n;
        int ans = 0x3f3f3f3f;
        while(l <= r){
            int mid = (l + r) >> 1;
            if(check(mid)){
                ans = min(ans,mid);
                r = mid - 1;
            }
            else l =  mid + 1;
        }
        if(ans == 0x3f3f3f3f)
            printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值