热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CodeforcesContest1073problemCVasyaandRobot——尺取

VasyahasgotarobotwhichissituatedonaninfiniteCartesianplane,initiallyinthecell(0,0).Robotca

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
inputCopy
5
RURUU
-2 3
outputCopy
3
inputCopy
4
RULR
1 1
outputCopy
0
inputCopy
3
UUU
100 100
outputCopy
-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).

题意:

给你一个长度为n的串,仅含有4种字符
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).
差不多这个意思,就是移动的位置。
起始位置是0,0,然后给你一个目标位置,问你不可以增加,不可以减小这个串的长度,仅可以在其中选一个区间修改内容,问你到目标位置最少需要改的长度是多少。

题解:

首先特判目标位置的x+y大于n和目标位置与初始到达的位置是一样的情况,之后就是尺取,以前我都是用while来做,但是听说for一遍r会更好一点?那就试试看。每一个r我们找最大的l,让(r-l+1)这个长度是能够从当前位置到达目标位置的长度,但是要判断这两个是否奇偶,举个例子:
6
RLRLRL
1 0
这种情况怎么样都不可能到达了,因为(r-l+1)与位置偏差大小不是同奇偶的。

#include
using namespace std;
const int N=2e5+5;
char s[N];
mapmp;
int main()
{int sx&#61;0,sy&#61;0;int n;scanf("%d",&n);scanf("%s",s&#43;1);int fx,fy;scanf("%d%d",&fx,&fy);if(abs(fx)&#43;abs(fy)>n)return 0*printf("-1\n");for(int i&#61;1;i<&#61;n;i&#43;&#43;){if(s[i]&#61;&#61;&#39;U&#39;)sy&#43;&#43;;else if(s[i]&#61;&#61;&#39;D&#39;)sy--;else if(s[i]&#61;&#61;&#39;L&#39;)sx--;elsesx&#43;&#43;;}if(sx&#61;&#61;fx&&sy&#61;&#61;fy)return 0*printf("0\n");int l&#61;1,minn&#61;n&#43;1;for(int r&#61;1;r<&#61;n;r&#43;&#43;){if(s[r]&#61;&#61;&#39;U&#39;)sy--;else if(s[r]&#61;&#61;&#39;D&#39;)sy&#43;&#43;;else if(s[r]&#61;&#61;&#39;L&#39;)sx&#43;&#43;;elsesx--;while(l<&#61;r&&abs(fx-sx)&#43;abs(fy-sy)<&#61;r-l&#43;1){if(((abs(fx-sx)&#43;abs(fy-sy)))%2&#61;&#61;(r-l&#43;1)%2)minn&#61;min(minn,r-l&#43;1);if(s[l]&#61;&#61;&#39;U&#39;)sy&#43;&#43;;else if(s[l]&#61;&#61;&#39;D&#39;)sy--;else if(s[l]&#61;&#61;&#39;L&#39;)sx--;elsesx&#43;&#43;;l&#43;&#43;;}}printf("%d\n",minn&#61;&#61;n&#43;1?-1:minn);return 0;
}


推荐阅读
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • C++中的三角函数计算及其应用
    本文介绍了C++中的三角函数的计算方法和应用,包括计算余弦、正弦、正切值以及反三角函数求对应的弧度制角度的示例代码。代码中使用了C++的数学库和命名空间,通过赋值和输出语句实现了三角函数的计算和结果显示。通过学习本文,读者可以了解到C++中三角函数的基本用法和应用场景。 ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有