Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】
任意门:http://codeforces.com/contest/1073/problem/C
C. 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) 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 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).
题意概括:
输入N个操作,询问修操作是否能到达终点,如果能到达终点输出“修改的区间”;
修改区间的定义:修改的最大坐标操作的坐标 - 修改的最小坐标操作的坐标
【规定起点为 (0,0),输入终点( x, y );】
操作(上下左右):
- 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)
解题思路:二分 || 尺取
预处理路径前缀和(压缩路径)sum_x [ ],sum_y[ ] ;则sum_x[ N ] , sum_y[ N ] 为实际的终点;
输入的终点为 (ex, ey),假设能修改若干个操作到达输入的终点,那么:
某一段 [ st, ed ] 所走的影响为:
X轴方向:xx = ex - ( sum_x[ N ] - sum_x[ ed -1 ] + sum_x[ st - 1 ] )
Y轴方向:yy = ey - ( sum_y[ N ] - sum_y[ ed - 1 ] + sum_y[ st - 1 ] )
二分
二分修改区间长度 len ,尺取判断在该长度是否满足修改条件;
①操作所走最大范围不得超过 len ,因为每次操作只是上下左右移动一步
②判断能否完成假设的影响 len - abs(xx) - abs(yy))%2 ?= 0;
abs(xx)表示的是 x 方向 到达终点 ex 的差值
abs(yy)表示的是 y 方向 到达终点 ey 的差值
假如 len > abs(xx)+abs(yy) 说明这段区间有操作是多余的,但是只要剩下的操作数是偶数就可以两两抵消。
尺取:
直接定义一个头指针一个尾指针,暴力一遍,条件判断是要头指针加还是尾指针加,记录最小修改区间。
AC code:
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+;
int x[N],y[N];
int sx,sy,n;
char s[N];
bool check(int m)
{
for(int i=;i<=n-m+;i++)
{
int tx=x[n]-x[i+m-]+x[i-]; //当前原来选项造成的横坐标影响
int ty=y[n]-y[i+m-]+y[i-]; //当前原来选项造成的纵坐标影响
int ex=sx-tx, ey=sy-ty; //消除当前影响
printf("%d %d m: %d\n",ex,ey,m);
if(m>=(abs(ex)+abs(ey)) && (m-abs(ex)-abs(ey))%==) //可以构造
return ;
}
return ;
}
int main()
{
//string s;
while(~scanf("%d",&n))
{
scanf("%s",s+);
x[]=y[]=;
for(int i=;i<=n;i++)
{
x[i]=x[i-];y[i]=y[i-]; //累积前面步数的结果 if(s[i]=='L') x[i]-=; //当前步数造成的影响
else if(s[i]=='R') x[i]+=;
else if(s[i]=='D') y[i]-=;
else y[i]+=;
}
// printf("%d %d\n",x[n],y[n]);
scanf("%d %d",&sx,&sy); //终点
// printf("HH");
int l=,r=n,ans=-;
while(l<=r) //二分答案
{
printf("l:%d r:%d\n", l, r);
int mid=(l+r)>>;
if(check(mid)) ans=mid,r=mid-;
else l=mid+;
}
printf("%d\n",ans);
}
}
Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】的更多相关文章
- Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分
题目:题目链接 思路:对于x方向距离与y方向距离之和大于n的情况是肯定不能到达的,另外,如果n比abs(x) + abs(y)大,那么我们总可以用UD或者LR来抵消多余的大小,所以只要abs(x) + ...
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot
题意:给出一段操作序列 和目的地 问修改(只可以更改 不可以删除或添加)该序列使得最后到达终点时 所进行的修改代价最小是多少 其中代价的定义是 终点序号-起点序号-1 思路:因为代价是终点序号减去 ...
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot(二分或者尺取)
题目哦 题意:给出一个序列,序列有四个字母组成,U:y+1,D:y-1 , L:x-1 , R:x+1; 这是规则 . 给出(x,y) 问可不可以经过最小的变化这个序列可以由(0,0) 变到(x, ...
- Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)
这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...
- Educational Codeforces Round 53 (Rated for Div. 2)
http://codeforces.com/contest/1073 A. Diverse Substring #include <bits/stdc++.h> using namespa ...
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)
题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...
- [codeforces][Educational Codeforces Round 53 (Rated for Div. 2)D. Berland Fair]
http://codeforces.com/problemset/problem/1073/D 题目大意:有n个物品(n<2e5)围成一个圈,你有t(t<1e18)元,每次经过物品i,如果 ...
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum
https://codeforces.com/contest/1073/problem/E 题意 求出l到r之间的符合要求的数之和,结果取模998244353 要求:组成数的数位所用的数字种类不超过k ...
- Educational Codeforces Round 53 (Rated for Div. 2)G. Yet Another LCP Problem
题意:给串s,每次询问k个数a,l个数b,问a和b作为后缀的lcp的综合 题解:和bzoj3879类似,反向sam日神仙...lcp就是fail树上的lca.把点抠出来建虚树,然后在上面dp即可.(感 ...
随机推荐
- win10 sshsecureshellclient删除profile保存的信息
C:\Users\joe\AppData\Roaming\SSH
- elastic 常用查询操作
_ GET http://127.0.0.1:9200/_cat/health?v 健康状况 GET http://127.0.0.1:9200/_cat/indices?v ...
- 【程序员技术练级】学习一门脚本语言 python(二)遍历本地文件系统
这篇将讲述怎么使用python来遍历本地文件系统,并把文件按文件大小从小到大排序的一个小例子 在这个例子中,主要会用到python内置的和OS模块的几个函数: os.walk() : 该方法用来遍历指 ...
- JAVA 利用反射自定义数据层框架
之前的随笔一直都在介绍c#,主要公司最近的业务都是做桌面程序,那么目前c#中的WPF肯定是我做桌面程序的不二之选,做了半年的WPF,也基本摸清了c#写代码的套路和规则(本人之前是两年多的JAVA开发者 ...
- 数据降维(Dimensionality reduction)
数据降维(Dimensionality reduction) 应用范围 无监督学习 图片压缩(需要的时候在还原回来) 数据压缩 数据可视化 数据压缩(Data Compression) 将高维的数据转 ...
- nyoj 211——Cow Contest——————【floyd传递闭包】
Cow Contest 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...
- 如何优雅地进行错误处理(clean code阅读笔记之六)
错误处理是十分必要的,但是如果对错误处理使用不当则会让代码变得十分臃肿,让阅读者看不清代码的逻辑,更严重的是,这也会让程序变得十分脆弱.本文中将列出一些使用错误处理的技巧,帮助你写出既简洁又健壮的代码 ...
- CSS3动画属性animation的基本用法
CSS3新增了一个令人心动的属性:animation,尽管利用animation做出来的动画没有flash或者javascript做出的动画流畅绚丽,但是从代码量和浏览器性能上有着明显的优点. ani ...
- js使用占位符替换字符串
js使用占位符替换字符串是一个ES6中的模版字符串语法. 在``中使用 ${} var a = 5; var b = 10; console.log(`Fifteen is ${a + b} and ...
- java字节码速查笔记
java字节码速查笔记 发表于 2018-01-27 | 阅读次数: 0 | 字数统计: | 阅读时长 ≍ 执行原理 java文件到通过编译器编译成java字节码文件(也就是.class文件) ...