CF 1073C Vasya and Robot(二分答案)
1 second
256 megabytes
standard input
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)(x,y) to (x,y+1)(x,y+1);
- D — move from (x,y)(x,y) to (x,y−1)(x,y−1);
- L — move from (x,y)(x,y) to (x−1,y)(x−1,y);
- R — move from (x,y)(x,y) to (x+1,y)(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: maxID−minID+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 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)(x,y), or tell him that it's impossible.
The first line contains one integer number n (1≤n≤2⋅105)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 (−109≤x,y≤109)x,y (−109≤x,y≤109) — the coordinates of the cell where the robot should end its path.
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.
- 5
- RURUU
- -2 3
- 3
- 4
- RULR
- 1 1
- 0
- 3
- UUU
- 100 100
- -1
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)(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).
【题意】
一个机器人从(0,0)出发,输入一段指令字符串,和机器人需要在指定步数后到达的终点,问如果机器人需要在指定步数内到达终点,那么需要对原指令字符串做出怎样的改变,假设改变 字符串的最大下标为maxindex,改变字符串的最小下标为minindex,输出最小的 maxindex-minindex+1,即,输出最小的改变字符串的连续区间长度(该区间内的字符不一定要全部发生改变)
【分析】
首先考虑在什么情况下,无论如何改动这个字符串都不能到达指定位置
1、字符串长度小于从原点到指定位置的距离
2、字符串长度与从原点到指定位置的奇偶性不同
在除去这两种情况下,剩余的情况都一定有答案。鉴于其可能解时连续的整数,因此,可以用二分枚举所有可能,进而找出最小的连续区间长度。
应注意,当根据给定字符串移动就能到达指定位置,即最小区间为0时,应排除在二分枚举的情况之外。
实际写代码时,特殊情况可以被包含于普通情况。但可以作为思路的引子。
当枚举长度为 x 时,考虑在 string 中所有长度为 x 的子串,是否存在一个子串可行。若存在,尝试缩短子串长度;若不存在,延长子串长度。
判断子串是否可行的方法:
设全集为给定字符串,沿着子串的补集移动,记这样移动到的点为 pos 。求 pos 到 指定位置 的距离,记为 d ,记子串的长度为 len。满足如下两种情况,则子串可行。
1、d <= len
2、(len-d)%2==0
【代码】
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=2e5+5;
int n,ex,ey,sx[N],sy[N];char s[N];
inline bool check(int m){//假定最佳区间长度为m
for(int i=1;i+m-1<=n;i++){
int decx=sx[n]-sx[i+m-1]+sx[i-1];
int decy=sy[n]-sy[i+m-1]+sy[i-1];
//不需要改变的区间恒存在的贡献
int nedx=ex-decx;
int nedy=ey-decy;
//需要改变的区间中,x和y想要到达终点,所需恰好作出的贡献
if(abs(nedx)+abs(nedy)<=m&&!(m-abs(nedx)-abs(nedy)&1)) return 1;
//(abs(tx)+abs(ty)位字符做出使该人刚好到达终点的贡献,
//剩下位的字符如果是偶数,就可以让其多走的路程两两抵消,从而刚好到达终点
}
return 0;
}
int main(){
scanf("%d%s%d%d",&n,s+1,&ex,&ey);
for(int i=1;i<=n;i++){
sx[i]=sx[i-1]+(s[i]=='L'?-1:(s[i]=='R'?1:0));
sy[i]=sy[i-1]+(s[i]=='D'?-1:(s[i]=='U'?1:0));
}
int l=0,r=n,mid,ans=-1;
while(l<=r){
mid=l+r>>1;
if(check(mid)){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
printf("%d\n",ans);
return 0;
}
CF 1073C Vasya and Robot(二分答案)的更多相关文章
- Codeforces 1073C Vasya and Robot 【二分】
<题目链接> 题目大意: 一个机器人从(0,0)出发,输入一段指令字符串,和机器人需要在指定步数后到达的终点,问如果机器人需要在指定步数内到达终点,那么需要对原指令字符串做出怎样的改变,假 ...
- Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分
题目:题目链接 思路:对于x方向距离与y方向距离之和大于n的情况是肯定不能到达的,另外,如果n比abs(x) + abs(y)大,那么我们总可以用UD或者LR来抵消多余的大小,所以只要abs(x) + ...
- C. Vasya and Robot二分
1.题目描述 Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell ...
- CF 672D Robin Hood(二分答案)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- cf C. Vasya and Robot
http://codeforces.com/contest/355/problem/C 枚举L和R相交的位置. #include <cstdio> #include <cstring ...
- Codeforces 1073C:Vasya and Robot(二分)
C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...
- 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 mem ...
- CF 371C-Hamburgers[二分答案]
C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Cf Round #403 B. The Meeting Place Cannot Be Changed(二分答案)
The Meeting Place Cannot Be Changed 我发现我最近越来越zz了,md 连调程序都不会了,首先要有想法,之后输出如果和期望的不一样就从输入开始一步一步地调啊,tmd现在 ...
随机推荐
- c#解析XML文件来获得pascal_voc特定目标负样本
近期在做船仅仅识别方面的事情,须要大量的负样本来训练adaboost分类器. 我从网上下载到一个pascal_voc的数据集.须要找到不包括船仅仅的那些复制出来. 数据集特点 对于每一个图片有一个xm ...
- eclipse断点Source not found解决方案1,2,3
1.tomcat插件 路径是Window --> Preferences --> Tomcat --> Source Path,在Source Path 标签下有行文字:Add ja ...
- 【玩转Golang】beego下实现martini中的透明式静态文件服务(static folder)效果。
出于效率等原因,最近将web框架由martini切换为了beego,其他地方都很平顺,只是两个框架的handler签名不一致,需要修改,所以耗时较长,这是预计到的.但是有一个地方没有预计到,也耗费了较 ...
- Eclipse + PyDev 快捷键
●多行缩进(减少缩进):tab/shift+tab ●复制行: Ctrl+Alt+方向键'↓' ●删除行:Ctrl+d ●自动完成:Alt+/ ●注释:Ctrl+/ ●窗口最大小:Ctrl+m == ...
- centos 离线安装Ambari
1.首先要下载它们的安装包,别尝试着在yum来下,特别慢,最好的方式是通过建立本地资源的方式来安装. http://public-repo-1.hortonworks.com/HDP/centos6/ ...
- SSM是什么框架?
SSM框架,是Spring + Spring MVC + MyBatis的缩写,这个是继SSH之后,目前比较主流的Java EE企业级框架,适用于搭建各种大型的企业级应用系统. 1.Spring简介 ...
- lua正则表达式如何匹配中文
function CheckChinese(s) local ret = {}; local f = '[%z\1-\127\194-\244][\128-\191]*'; local line, l ...
- Python打包-Pyinstaller
我们知道,Python很优雅,很值得学习.但是Python是解释性语言,代码需要有Python解释器才能执行,相比较我们平时直接运行exe等可执行文件多了一步的麻烦. 于是,希望能将Python程序打 ...
- webApi2 上传大文件代码
上传大文件,取消内存缓存: GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), ...
- Tomcat多站点部署方式
1.同一个Tomcat 同一个端口 部署多个项目 第一步:把待发布的多个项目.war文件放入tomcat的webapps文件下下 第二步:修改 $TOMCAT_HOME\conf 下的server.x ...