[codefoeces] Educational Codeforces Round 60 C. Magic Ship

time limit per test
2 seconds
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 (?1,?1)(x1,y1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (?2,?2)(x2,y2).

You know the weather forecast — the string ?s of length ?n, 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 ?1s1, the second day — ?2s2, the ?n-th day — ??sn and (?+1)(n+1)-th day — ?1s1 again and so on.

Ship coordinates change the following way:

  • if wind blows the direction U, then the ship moves from (?,?)(x,y) to (?,?+1)(x,y+1);
  • if wind blows the direction D, then the ship moves from (?,?)(x,y) to (?,?1)(x,y−1);
  • if wind blows the direction L, then the ship moves from (?,?)(x,y) to (?1,?)(x−1,y);
  • if wind blows the direction R, then the ship moves from (?,?)(x,y) to (?+1,?)(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) it will move to the point (?1,?+1)(x−1,y+1), and if it goes the direction U, then it will move to the point (?,?+2)(x,y+2).

You task is to determine the minimal number of days required for the ship to reach the point (?2,?2)(x2,y2).

Input

The first line contains two integers ?1,?1x1,y1 (0?1,?11090≤x1,y1≤109) — the initial coordinates of the ship.

The second line contains two integers ?2,?2x2,y2 (0?2,?21090≤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 ?n (1?1051≤n≤105) — the length of the string ?s.

The fourth line contains the string ?s 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 (?2,?2)(x2,y2).

If it's impossible then print "-1".

Examples
input
Copy
0 0
4 6
3
UUU
output
Copy
5
input
Copy
0 3
0 0
3
UDD
output
Copy
3
input
Copy
0 0
0 1
1
L
output
Copy
-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).

 

二分答案

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N = 1e5+7;
 5 int n;
 6 int x1,x2,Y1,y2,lenx,leny;
 7 int curx[N],cury[N];
 8 char s[N];
 9 
10 bool check(ll ans){
11     ll x = (ll)curx[n] * (ans/n);
12     x += curx[ans%n];
13     ll y = (ll)cury[n] * (ans/n);
14     y += cury[ans%n];
15     if (abs(lenx - x) + abs(leny - y) <= ans) return 1;
16     return 0;
17 }
18 
19 ll binsearch(ll l,ll r){
20     while(l < r){
21         ll mid = (l + r) >> 1;
22         if (check(mid)){
23             r = mid;
24         }else l = mid+1;
25     }
26     return (l == 1e18) ? -1 : l;
27 }
28 
29 int main(){
30     scanf("%d%d%d%d%d",&x1,&Y1,&x2,&y2,&n);
31     lenx = x2 - x1;
32     leny = y2 - Y1;
33     getchar();
34     for (int i = 1;i <= n;++i){
35         scanf("%c",s+i);
36         curx[i] = curx[i-1],cury[i] = cury[i-1];
37         switch (s[i]){
38             case 'U': cury[i]++;break;
39             case 'D': cury[i]--;break;
40             case 'L': curx[i]--;break;
41             case 'R': curx[i]++;break;
42         }
43     }
44     cout << binsearch(0,1e18) << endl;
45     return 0;
46 }

 

转载于:https://www.cnblogs.com/mizersy/p/10400506.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值