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 (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:
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
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).
题意:一个船要从起点到终点,一个字符串代表风向每天的上下左右,每天船还可以选择再走一步,问至少几天到终点。
思路:首先风向和选择走的可以分离考虑,那么可以二分答案,先走K天,风向的终点先算出来,那么剩下K步自由发挥,判断距离目的地的曼哈顿距离够不够K就行。二分上界每一个轮回至少朝向目的地走一步,那么存在答案的话2e14就一定能到达。
# include
using namespace std;
typedef long long LL;
const int maxn = 1e5+30;
char s[maxn];
int x[maxn], y[maxn];
int x0, y0, x1, y1, n;
bool check(LL k){
if(k < 0) return false;
LL d = k/n, r = k%n;
LL dx = 1LL*d*x[n]+x[r]+x0, dy = 1LL*d*y[n]+y[r]+y0;
LL tot = llabs(dx-x1)+llabs(dy-y1);
return k >= tot;
}
int main(){
scanf("%d%d%d%d%d",&x0,&y0,&x1,&y1, &n);
scanf("%s",s+1);
for(int i=1; i<=n; ++i){
x[i] = x[i-1];
y[i] = y[i-1];
if(s[i] == 'U') ++y[i];
else if(s[i] == 'D') --y[i];
else if(s[i] == 'L') --x[i];
else ++x[i];
}
LL l=0, r=4e17;
while(l<=r){
LL mid = l+r>>1;
if(check(mid)) r = mid-1;
else l = mid+1;
}
if(check(l)) printf("%lld\n",l);
else puts("-1");
return 0;
}