CodeForce Educational round Div2 C - Vasya and Robot

Posted lecoz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForce Educational round Div2 C - Vasya and Robot相关的知识,希望对你有一定的参考价值。

C. Vasya and Robot
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

  • U — move from (??,??)(x,y) to (??,??+1)(x,y+1);
  • D — move from (??,??)(x,y) to (??,???1)(x,y?1);
  • L — move from (??,??)(x,y) to (???1,??)(x?1,y);
  • R — move from (??,??)(x,y) to (??+1,??)(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: ?????????????????????+1maxID?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 22, 55 and 77 are changed, so the length of changed subsegment is 7?2+1=67?2+1=6. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is 11.

If there are no changes, then the length of changed subsegment is 00. 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)(0,0) to (??,??)(x,y), or tell him that it‘s impossible.

Input

The first line contains one integer number ?? (1??2?105)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 ??,?? (?109??,??109)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)(0,0) to (??,??)(x,y). If this change is impossible, print ?1?1.

Examples
input
Copy
5
RURUU
-2 3
output
Copy
3
input
Copy
4
RULR
1 1
output
Copy
0
input
Copy
3
UUU
100 100
output
Copy
-1
Note

In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is 3?1+1=33?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 00.

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

 

思路:

先分别求x轴,y轴上的前缀和,偏于之后判断区间是否满足条件。详细见代码。

固定左端点,二分枚举右端点。判断左右端点的区间是否能够达成目标(区间长度是否大于未完成量,奇偶性是否相同)

注意点:

之前没遇到过固定一个点,然后另一个点二分逼近值的。

 

技术分享图片
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#define ll long long
#define local

using namespace std;

const int MOD = 1e9+7;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 2e5+10;

int n;
ll endx, endy;
ll total;
char str[maxn];
int sumx[maxn];
int sumy[maxn];

bool ok(int l, int r) {
    int nowx = sumx[n-1]-sumx[r];
    int nowy = sumy[n-1]-sumy[r];
    if (l > 0) {
        nowx += sumx[l-1];
        nowy += sumy[l-1];
    }
    ll diff = abs(endx-nowx)+abs(endy-nowy);
    if (diff <= r-l+1 && ((diff&1))==((r-l+1)&1)) return 1;
    else return 0;
}

int main() {
#ifdef local
    if(freopen("/Users/Andrew/Desktop/data.txt", "r", stdin) == NULL) printf("can‘t open this file!
");
#endif
    scanf("%d", &n);
    scanf("%s", str);
    scanf("%lld%lld", &endx, &endy);
    total = abs(endx)+abs(endy);//总的步数
    //如果两者奇偶性不同也不行
    if (total>n || ((total&1) != (n&1))) {
        printf("-1
");
        return 0;
    }
    sumx[0] = 0;
    sumy[0] = 0;
    int len = int(strlen(str));//strlen(str)是O(n)的复杂度 orz...
    for (int i = 0; i < len; ++i) {
        int incx  = 0; int incy = 0;
        if (str[i] == U) {
            incy = 1;
        } else if (str[i] == R) {
            incx = 1;
        } else if (str[i] == D) {
            incy = -1;
        } else {
            incx = -1;
        }
        if (i) {
            sumx[i] += sumx[i-1]+incx;
            sumy[i] += sumy[i-1]+incy;
        }
        else {
            sumx[i] = incx;
            sumy[i] = incy;
        }
    }
    if (sumx[n-1]==endx && sumy[n-1]==endy) {
        printf("0
");
        return 0;
    }
    int mn = inf;
    //枚举点
    for (int i = 0; i < n; ++i) {
        int l = i-1; int r = n-1;
        while (r-l > 1) {
            int m = (l+r)>>1;
            if (ok(i, m)) {
                r = m;
            } else {
                l = m;
            }
        }
        //判断一下r是否可行
        if (ok(i, r)) mn = min(mn, r-i+1);
    }
    printf("%d
", mn);
#ifdef local
    fclose(stdin);
#endif
    return 0;
}
View Code

 







以上是关于CodeForce Educational round Div2 C - Vasya and Robot的主要内容,如果未能解决你的问题,请参考以下文章

codeforce 621B Wet Shark and Bishops

codeforce 随机挑战 题解

CodeForce 113B DP

codeforce B

(Codeforce)Correct Solution?

[CodeForce721C]Journey