zoukankan      html  css  js  c++  java
  • 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 (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).

    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 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
    %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
    ",ans);
    59         return 0;
    60     }
    View Code
  • 相关阅读:
    JSP ——第九次作业
    JSP ——第八次作业
    JSP ——第七次作业-mail_system
    JSP ——第六次作业
    JSP——第五次作业
    软件测试——第二次
    JSP作业 四——2
    JSP 作业 四 —(1)
    JSP 第三次作业
    NSData的同步下载与NSConnection的同步下载
  • 原文地址:https://www.cnblogs.com/yitou13/p/9942759.html
Copyright © 2011-2022 走看看