Div. 2 ,C. Vasya and Robot

2024-03-20 17:32
文章标签 div robot vasya

本文主要是介绍Div. 2 ,C. Vasya and Robot,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这个题目第一眼看并不是一个二分,仔细思考一下,要枚举长度,然后又要枚举起点,这里已经是O(n^2)的复杂度了,还要判断是否满足条件,如果预处理了,是能在O(1)的复杂度解决的。所以枚举长度这边可以用一个二分,这样复杂度就是O(nlogn)了,可以解决这个题目。

如何在O(n)的复杂度判断这个长度满不满足要求呢?

枚举起点就要O(n)了,接下来就是O(1)判断O不OK;预处理到第i个字母时往右往上偏移了几位,那么枚举到一段区间的时候,我们可以将那一段区间的操作全部撤销(反正全改,长度也是len),那么此时的位置移到目标点最少的操作num 就是现在的位置和终点在X轴上的偏移量+Y轴上的偏移量,如果len>num,那么len-num一定要是偶数,这样上下左右才可以抵消。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,ex,ey;
const int maxn=2e6+5;
struct X{ll r,u;
}f[maxn]; 
bool flag=0;
bool pd(int len){for(int i=1;i+len-1<=n;++i){ll rr=f[i+len-1].r-f[i-1].r,uu=f[i+len-1].u-f[i-1].u;//区间r和u的偏移量ll num=abs(ex-(f[n].r-rr))+abs(ey-(f[n].u-uu));//所需要的最小操作数if(len>=num&&(len-num)%2==0){flag=1;//说明可以走到终点return 1;}}return 0;
}
int main(){scanf("%lld",&n);getchar();f[0].r=0,f[0].u=0;char tmp;for(int i=1;i<=n;++i){scanf("%c",&tmp);//预处理if(tmp=='R')	f[i].r=f[i-1].r+1,f[i].u=f[i-1].u;if(tmp=='L')	f[i].r=f[i-1].r-1,f[i].u=f[i-1].u;if(tmp=='U')	f[i].u=f[i-1].u+1,f[i].r=f[i-1].r;if(tmp=='D')	f[i].u=f[i-1].u-1,f[i].r=f[i-1].r;}getchar();scanf("%lld%lld",&ex,&ey);if(f[n].r==ex&&f[n].u==ey)	cout<<0<<endl;else{int le=0,ri=n+1;while(ri>le){int mid=(le+ri)/2;if(pd(mid))	ri=mid;else	le=mid+1;}if(!flag)	cout<<-1<<endl;else	cout<<le<<endl;}return 0;
}

 

这篇关于Div. 2 ,C. Vasya and Robot的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/830222

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

创建一个大的DIV,里面的包含两个DIV是可以自由移动

创建一个大的DIV,里面的包含两个DIV是可以自由移动 <body>         <div style="position: relative; background:#DDF8CF;line-height: 50px"> <div style="text-align: center; width: 100%;padding-top: 0px;"><h3>定&nbsp;位&nbsp;

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

CF#271 (Div. 2) D.(dp)

D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/474/problem/D We s

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There

CF#278 (Div. 2) A.(暴力枚举)

A. Giga Tower time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/A Giga To

CF #283 (Div. 2) B.(字符串好多坑)

题目链接:http://codeforces.com/contest/496/problem/B 解题思路: 首先明确可以暴力,写一个add函数和shift函数。然后给一个循环值cnt,做cnt次循环即可,每次取两种情况的最小值入字符串数组,最后排下序,输出最小的字符串。 这个cnt很不好控制,我也是WA了几次才估计出来的······· 接下来说说奇葩的数据: Input: 1