[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 }

 

posted @ 2019-02-19 13:31  mizersy  阅读(273)  评论(0编辑  收藏  举报