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

5RURUU-2 3

output

Copy

3

input

Copy

4RULR1 1

output

Copy

0

input

Copy

3UUU100 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!\n");
#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\n");
        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\n");
        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\n", mn);
#ifdef local
    fclose(stdin);
#endif
    return 0;
}

原文地址:https://www.cnblogs.com/lecoz/p/9919854.html

时间: 2024-10-11 07:02:18

CodeForce Educational round Div2 C - Vasya and Robot的相关文章

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

任意门:http://codeforces.com/contest/1073/problem/C 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, i

Educational Codeforces Round 26 E - Vasya&#39;s Function

数论题还是好恶心啊. 题目大意:给你两个不超过1e12的数 x,y,定义一个f ( x, y ) 如果y==0 返回 0 否则返回1+ f ( x , y - gcd( x , y ) ); 思路:我们设gcd ( x , y) 为G,那么 设 x  = A*G,y = B*G,我们考虑减去多少个G时x y 的gcd会改变,我们设减去 k个G的时候 x和y 的gcd为改变,即 A*G 和 ( B - k ) * G 的 gcd 改变了,什么情况下会改变呢,就是A 和( B -  k )的gcd

[Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]

咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\(n\)的严格单调上升数组\(d_i\),求\(\max\limits_{1 \le l \le r \le n} (a\cdot(r-l+1)-\sum_{i=l}^{r}c_i-gap(l,r))\),其中\(gap(l, r) = \max\limits_{l \le i < r} (d_{i

codeforces educational round 73 div2 ABCD

A. 2048 Game Description 给出n个2的幂次数,问能否累加得到2048 Solution 从小往上累加. B. Knights Description Solution 贪心构造. 奇数位和偶数位放置不同就行了. C. Perfect Team Description 给出三个数,$c,m,x$代表每种选手的个数. 每三个人组一队,每队要求至少有一个c,一个m. 问最多能组多少支队伍. Solution 显然最多只能组成$min(c,m)$只队伍. 这是在$x \geq m

Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) C. Vasya and Golden Ticket

C. Vasya and Golden Ticket time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Recently Vasya found a golden ticket - a sequence which consists of nn digits a1a2-ana1a2-an. Vasya considers a ti

【Codeforces Round 1132】Educational Round 61

Codeforces Round 1132 这场比赛做了\(A\).\(B\).\(C\).\(F\)四题,排名\(89\). \(A\)题\(wa\)了一次,少考虑了一种情况 \(D\)题最后做出来,但被\(hack\)了...被\(hack\)的原因是没有想到答案会超过\(10^{12}\)(毕竟这个时间上的优化也是在最后迫不得已的情况下加的,就没有考虑正确性... Codeforces 1132 C 题意:给一些区间\([l_i,r_i]\),从中删掉两个,求剩下的区间最多能够覆盖的格子数

【Codeforces】Educational Round 61

今天刚打完一场,心情异常烂,只做出来一道题,开错题了然后没钻出来,机房的小伙伴都切了四道题了...可能又要掉了,要继续努力了. 这次Edu Round比赛是这周第二次在宿舍请假了,等网安结束了就退宿吧. 比较顺利的做出了ABC,然后看人数去推了F题,不过没什么结果. 然后在改代码的时候结识了白学长,:) 话说这几天好燥啊,G题其实代码挺好理解但愣是拖了两天啊. Problem A. Regular Bracket Sequence 题目大意:现在给出 4 种括号依次为 "((" , &

Codeforces Round #575 (Div. 3) C. Robot Breakout (模拟,实现)

C. Robot Breakout time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output n robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental,

Experimental Educational Round: VolBIT Formulas Blitz D

Description After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might & Magic". A part of t