Educational Codeforces Round 60 (Rated for Div. 2) 即Codeforces Round 1117 C题 Magic Ship
time limit per test 2 second
memory limit per test 256 megabytes
input standard input
output standard output
You a captain of a ship. Initially you are standing in a point (x1,y1)(x1,y1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (x2,y2)(x2,y2).
You know the weather forecast — the string ss of length nn, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side s1s1, the second day — s2s2, the nn-th day — snsn and (n+1)(n+1)-th day — s1s1 again and so on.
Ship coordinates change the following way:
if wind blows the direction U, then the ship moves from (x,y)(x,y) to (x,y+1)(x,y+1);
if wind blows the direction D, then the ship moves from (x,y)(x,y) to (x,y−1)(x,y−1);
if wind blows the direction L, then the ship moves from (x,y)(x,y) to (x−1,y)(x−1,y);
if wind blows the direction R, then the ship moves from (x,y)(x,y) to (x+1,y)(x+1,y).
The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction Uand the ship moves the direction L, then from point (x,y)(x,y) it will move to the point (x−1,y+1)(x−1,y+1), and if it goes the direction U, then it will move to the point (x,y+2)(x,y+2).
You task is to determine the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).
Input
The first line contains two integers x1,y1x1,y1 (0≤x1,y1≤1090≤x1,y1≤109) — the initial coordinates of the ship.
The second line contains two integers x2,y2x2,y2 (0≤x2,y2≤1090≤x2,y2≤109) — the coordinates of the destination point.
It is guaranteed that the initial coordinates and destination point coordinates are different.
The third line contains a single integer nn (1≤n≤1051≤n≤105) — the length of the string ss.
The fourth line contains the string ss itself, consisting only of letters U, D, L and R.
Output
The only line should contain the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).
If it's impossible then print "-1".
Examples
input
0 0
4 6
3
UUU
output
5
input
0 3
0 0
3
UDD
output
3
input
Copy
0 0
0 1
1
L
output
-1
Note
In the first example the ship should perform the following sequence of moves: "RRRRU". Then its coordinates will change accordingly: (0,0)(0,0) →→ (1,1)(1,1) →→ (2,2)(2,2) →→ (3,3)(3,3) →→ (4,4)(4,4) →→ (4,6)(4,6).
In the second example the ship should perform the following sequence of moves: "DD" (the third day it should stay in place). Then its coordinates will change accordingly: (0,3)(0,3) →→ (0,3)(0,3) →→ (0,1)(0,1) →→ (0,0)(0,0).
In the third example the ship can never reach the point (0,1)(0,1).
题目大意
一艘船,在$(sx,sy)$,要到$(tx,ty)$,船长需要考虑风向,每天刮一种方向的风,风向由字符串给出,第一天第一个字符,第二天第二个字符……第$n$天第$n$个字符,第$n+1$天回到第一个字符,即循环节长度$n$天,每天风会把船向下风方向吹开1单位长度,船上发动机可以把船向任意方向移动1单位长度(也可以选择不移动),问最少几天到达目的地。
解题思路
Neil看数据范围就猜到了二分答案,学长们也是脱口而出二分…菜是原罪…二分最终答案,那么最终答案就是由两个部分组成——几个完整循环节和最后的不到一个循环节的零碎部分。我们让船随风飘mid天,然后看看mid天以后距离终点有多远,如果这个距离小于等于mid,那么可以在这mid天内通过发动机补足这段距离,即mid大于等于答案,否则发动机每天都运转也补足不了,即mid小于答案。
曼哈顿距离就是好,两点间的最短路径固定,路线可以再两点画出的矩形内扭动。
源代码
#include<stdio.h>
#include<algorithm> long long sx,sy,tx,ty,dx[],dy[];
int n;
char s[]; int main()
{
scanf("%lld%lld%lld%lld%lld",&sx,&sy,&tx,&ty,&n);
scanf("%s",s+);
for(int i=;i<=n;i++)
{
dx[i]=dx[i-];dy[i]=dy[i-];
if(s[i]=='U') dy[i]++;
else if(s[i]=='D') dy[i]--;
else if(s[i]=='L') dx[i]--;
else dx[i]++;
}
long long dd=std::abs(dx[n])+std::abs(dy[n]);
long long l=,r=1LL<<;
while(l<r)
{
long long mid=l+r>>;
long long turn=mid/n;//轮数
long long rest=mid-turn*n;//零碎
long long x=sx+turn*dx[n]+dx[rest],y=sy+turn*dy[n]+dy[rest];
if(std::abs(x-tx)+std::abs(y-ty)<=mid)
r=mid;
else
l=mid+;
}
if(l==1LL<<) printf("-1\n");
else printf("%lld\n",l);
return ;
}
Educational Codeforces Round 60 (Rated for Div. 2) 即Codeforces Round 1117 C题 Magic Ship的更多相关文章
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
- Educational Codeforces Round 60 (Rated for Div. 2)
A. Best Subsegment 题意 找 连续区间的平均值 满足最大情况下的最长长度 思路:就是看有几个连续的最大值 #include<bits/stdc++.h> using n ...
- Educational Codeforces Round 60 (Rated for Div. 2)D(思维,DP,快速幂)
#include <bits/stdc++.h>using namespace std;const long long mod = 1e9+7;unordered_map<long ...
- Educational Codeforces Round 60 (Rated for Div. 2)E(思维,哈希,字符串,交互)
#include <bits/stdc++.h>using namespace std;int main(){ string t; cin>>t; int n=t.size() ...
- Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)
题目传送门 题意: 一个魔法水晶可以分裂成m个水晶,求放满n个水晶的方案数(mol1e9+7) 思路: 线性dp,dp[i]=dp[i]+dp[i-m]; 由于n到1e18,所以要用到矩阵快速幂优化 ...
- Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String
题目大意:这是一道交互题.给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3 ...
- Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students 水题
A. Two Rival Students There are
随机推荐
- Geometry Shader 实现 Wireframe 绘制边线的Shader
最终效果: 参考了一个免费插件 https://assetstore.unity.com/packages/vfx/shaders/directx-11/ucla-wireframe-shader-2 ...
- org.apache.poi.hssf.util.Region
从POI 3.18开始被Deprecated,在3.20版本中被移除了,所以3.20以前的都有 为了避免这个问题,用CellRangeAddress代替Region,其用法相同
- 【题解】自行车比赛 [AHOI2016] [P2777]
[题解]自行车比赛 \([AHOI2016]\) \([P2777]\) 逼自己每天一道模拟题 传送门:自行车比赛 \([AHOI2016]\) \([P2777]\) [题目描述] 比赛中一共有 \ ...
- 洛谷 P1045 麦森数
题目描述 形如2^{P}-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2^{P}-1不一定也是素数.到1998年底,人们已找到了37个麦森数.最大的一个是P=30213 ...
- C++ 类中的3种权限作用范围
三种访问权限 public:可以被任意实体访问 protected:只允许子类及本类的成员函数访问 private:只允许本类的成员函数访问 #include <iostream> #in ...
- Hdu 3487 play the chain
Description 瑶瑶很喜欢玩项链,她有一根项链上面有很多宝石,宝石从1到n编号. 首先,项链上的宝石的编号组成一个序列:1,2,3,...,n. 她喜欢两种操作: 1.CUT a b c:他会 ...
- 贪心 HDOJ 5355 Cake
好的,数据加强了,wa了 题目传送门 /* 题意:1到n分成m组,每组和相等 贪心:先判断明显不符合的情况,否则肯定有解(可能数据弱?).贪心的思路是按照当前的最大值来取 如果最大值大于所需要的数字, ...
- 网站开发综合技术 三 JavaScript的DOM操作
第3部分 JavaScript的DOM操作 1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Windows对象操作 ...
- javascript学习之Date对象
前几天学习了一下date对象,于是写了一个简单的时间显示放到博客页面里(位于右上角),类似这样的效果,时:分:秒 xxxx年xx月xx日. 下面来说一下具体实现步骤. 首先,既然date是一个对象,那 ...
- PHP语言开发Paypal支付demo的具体实现
如果我们的应用是面向国际的.那么支付的时候通常会考虑使用paypal.以下为个人写的一个paypal支付示例,已亲测可行.paypal有个很不错的地方就是为开发者提供了sandbox(沙箱)测试功能. ...