1. 程式人生 > >C. Vasya and Robot二分

C. Vasya and Robot二分

1.題目描述

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 (x,y+1)
  • D — move from (x,y)to 
1)">(x,y1)
  • L — move from (x,y)to (x1,y)
  • R — move from (x,y) to (x+1,y)
  • Vasya also has got a sequence of nn operations. Vasya wants to modify this sequence so after performing it the robot will end up in 

    (x,y)">(x,y)(x,y).

    Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: maxIDminID+1maxID−minID+1, where maxIDmaxID is the maximum index of a changed operation, and 

    inID">minIDminID 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 72+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)(x,y), or tell him that it's impossible.

    Input

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

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

    The third line contains two integers x,y (109x,y109)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)(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 31+1=33−1+1=3.

    In the second example the given sequence already leads the robot to (x,y)(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).

    2.思路:

    機器人行走的每一步先後順序其實是沒有意義的,這也是這道題的關鍵。

    先按照題目中給的路徑計算x,y移動的位置,再二分判斷修改的地方在哪裡。

    程式碼:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<vector>
     4 #include<map>
     5 #include<cstring>
     6 #include<cstdlib>
     7 #include<algorithm>
     8 #include<set>
     9 #include<cmath>
    10 using namespace std;
    11 const int SIZE = 200005;
    12 char s[SIZE];
    13 int ex,ey;
    14 int xSum[SIZE];
    15 int ySum[SIZE];
    16 int dx[200],dy[200];
    17 dy['U'] = 1;
    18 dy['D'] = -1;
    19 dx['L'] = -1;
    20 dx['R'] = 1;
    21 //判斷下一步在哪個區間段內行走 
    22 bool judge(int n, int len, int ex,int ey){
    23     for(int i = 1; i + len-1 <= n; ++i){
    24         int curx = xSum[i-1] + xSum[n]-xSum[i+len-1]; 
    25         // 去除長度 len長度 後的 x 走到的位置
    26         int cury = ySum[i-1] + ySum[n]-ySum[i+len-1]; 
    27         // 去除長度 len 長度後的 y走到的位置
    28         int delta = abs(curx-ex) + abs(cury-ey); 
    29         // 距離到達終點還需要多少步
    30         if(delta <= len && (len-delta)%2 == 0) // 到達終點還需要的步數 一定小於 目前可以通過改變方向的那些步數的個數 len
    31             return true;                       // 並且 因為此時 len兩端 必須是改變的(0 或者 1 是特殊情況) len與delta差值 必須為偶數才能到終點
    32     }
    33     return false;
    34 }
    35  
    36 int main()
    37     {
    38         int n;
    39         scanf("%d\n%s",&n,s+1);
    40         scanf("%d%d",&ex,&ey);
    41         //先算出題目給定路徑的最後位置 
    42         for(int i = 1; i <= n; ++i){
    43             xSum[i] = xSum[i-1] + dx[s[i]]; 
    44             ySum[i] = ySum[i-1] + dy[s[i]];
    45         }
    46         int lb = 0,ub = n;
    47         int ans = -1,mid;
    48         while(lb <= ub){
    49             mid = (lb+ub)/2;
    50             if(judge(n,mid,ex,ey)){
    51                 ans = mid;
    52                 ub = mid-1;
    53             }
    54             else{
    55                 lb = mid+1;
    56             }
    57         }
    58         printf("%d\n",ans);
    59         return 0;
    60     }
    View Code

     

     

    相關推薦

    Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot(二分+模擬)

    題目連結 題意 一個二維平面上有個機器人初始在 ( 0 ,

    Educational Codeforces Round 53 C Vasya and Robot (二分)

    題意 有一個機器人,有四種指令,上下左右,最初機器人在 ( 0 ,

    C. Vasya and Robot二分

    1.題目描述 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

    Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 二分+前後綴預處理

    題意: 給定長度為n的字串,每個字元表示朝上下左右四個方向前進,給定一個目標位置,找一個最小的區間,使得改變這個區間的若干個字元,使得整個串的操作能到達目標位置只需要輸出最小區間長度 思路: 首先暴力的想法就是列舉所有的區間,這樣這個區間兩邊就會有一些操作不會改變,然後我們根據這些操

    Codeforces C. Vasya and Robot+二分

    題目連結:http://codeforces.com/contest/1073/problem/C 題目大意:機器人有4種操作 U — (x,y)->(x,y+1) D — (x,y)->(x,y−1) L — (x,y)->(x−1,y) R — (x,y)->(x

    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

    【Educational Codeforces Round 53 (Rated for Div. 2)-C. Vasya and Robot二分

    Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 題意 在 二

    codeforces 1073 C. Vasya and Robot 1073D. Berland Fair(兩個思維題)

    C題: 題意:給你一串操作,你可以修改其中的操作使得小明從0 0 走到x y ,定義最小操作次數為最遠兩次修改操作的距離,你要求出最小操作次數或者輸出不可能。 思路: 沒啥疑問,直接二分judge就可以了。 D題: 題意:有n個糖果店,排成一個圈小明有T元錢,然後

    CF 1073C. Vasya and Robot(二分)

    題目連結 題目分析 二分,之前想多了,想成兩個方向沒有關係了,其實因為題目沒有問怎麼更改,可是我們可以想一下,我們判斷它無法到達的根據 n&lt;abs(x)+abs(y)∣∣(n−abs(x

    codeforces1073C. Vasya and Robot(二分+字首和)

                                                                                                          C. Vasya and Robot time limit per t

    Vasya and Robot- 二分搜尋- Educational Codeforces Round 53 (Rated for Div. 2)

    題目傳送門 題意: 機器人初始位置在(0,0),然後輸入一串指令有R,L,D,U,分別向x,y方向行走,R:x+1,L:x-1,U:y+1,D:y-1;。然後輸入目標位置,要你求需要改變的最短區間的長度

    Educational Codeforces Round 53 (Rated for Div. 2)-C. Vasya and Robot

    思路:一開始做思路就錯了,我考慮的是如何轉變方向使其有效到達目標位置,但是比較複雜。這題要求更改的區間最小長度。因此可以二分割槽間長度,在遍歷左端點看是否存在即可。判斷存在,即更改的區間的個數是否能將其他區域的缺少或多餘步驟給消掉即可。 Code: #includ

    Vasya and Robot CodeForces - 1073C (經典走方格問題+二分

    題意:機器人從起點(0,0)開始,每次只能向上,下,左,右進行移動,給出一個移動序列,問你通過修改這個移動序列,可以讓機器人走到(x,y)點的最小修改量(最小修改量定義為:修改的最大編號-最小編號+1)。 思路:將能否走到(x,y)轉化為偏移量能否從 0到 x,0到y.   &nb

    【Codeforces1073C】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 and Robot二分查詢)

    題目傳送: 題意: 給出一段錯誤路徑,現要求修改路徑,使得能夠成功到達目標座標。求最小修改量。  思路: 若最終能使改動後到達目標座標(tarx,tary),設改動區間長度len,記刪除len內容後的位置座標為(nowx,nowy),計算出(

    1073C 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 V

    codeforces 1073C Vasya and Robot二分查詢)

    題目描述: 傳送門 此題是對區間長度進行二分查詢。 對於一個區間長度為len的區間來說,算出沒加這塊區間的座標,然後算出與終點座標的絕對距離dis,如果dis<=len&&dis和len的奇偶性相同則可以走的目的地。然後縮小區間長度。否則增大區間長

    雙端隊列 C. Vasya and String

    character pop length class ica ger eno 隊列 chang High school student Vasya got a string of length n as a birthday present. This string co

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

    足夠 scan property ++ inter digits pos 通過 題意 C. Vasya and Golden Ticket time limit per test 1 second memory limit per test 256 megabytes

    C. Vasya and Multisets

    代碼 return 傳送門 偶數 n) def sync \n 方式 傳送門 [http://codeforces.com/contest/1051/problem/C] 題意 給你一堆數,問是否可以分為兩堆使得兩堆裏只出現一下的數字的種類相等,可以輸出任意一種分的方式 分