CodeForces 1117C 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小于答案。
曼哈顿距离就是好,两点间的最短路径固定,路线可以再两点画出的矩形内扭动。
源代码
1 #include<stdio.h> 2 #include<algorithm> 3 4 long long sx,sy,tx,ty,dx[100010],dy[100010]; 5 int n; 6 char s[100010]; 7 8 9 int main() 10 { 11 scanf("%lld%lld%lld%lld%lld",&sx,&sy,&tx,&ty,&n); 12 scanf("%s",s+1); 13 for(int i=1;i<=n;i++) 14 { 15 dx[i]=dx[i-1];dy[i]=dy[i-1]; 16 if(s[i]=='U') dy[i]++; 17 else if(s[i]=='D') dy[i]--; 18 else if(s[i]=='L') dx[i]--; 19 else dx[i]++; 20 } 21 long long dd=std::abs(dx[n])+std::abs(dy[n]); 22 long long l=1,r=1LL<<62; 23 while(l<r) 24 { 25 long long mid=l+r>>1; 26 long long turn=mid/n;//轮数 27 long long rest=mid-turn*n;//零碎 28 long long x=sx+turn*dx[n]+dx[rest],y=sy+turn*dy[n]+dy[rest]; 29 if(std::abs(x-tx)+std::abs(y-ty)<=mid) 30 r=mid; 31 else 32 l=mid+1; 33 } 34 if(l==1LL<<62) printf("-1\n"); 35 else printf("%lld\n",l); 36 return 0; 37 }