C. Magic Ship(经典二分)

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:

  • 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

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).

这个题是一个二分的好题呀。我刚开始的思路是先任由风吹,当某一时刻,当前位置到终点的距离小于当前时刻花费的时间的时候就将当前的时刻当成答案输出。有了这基本思想后就可以采用二分时间即可了。

0到1e18二分是非常快的。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll a[5],x1,y11,x2,y2,n;
int dx[N],dy[N];
string s;
bool valid(ll m)
{
	ll a=m/n;
	ll b=m%n;
	ll x=a*dx[n]+dx[b]+x1;
	ll y=a*dy[n]+dy[b]+y11;
	ll ans=abs(x-x2)+abs(y-y2);
	return ans<=m;
}
int main()
{
	cin>>x1>>y11>>x2>>y2>>n>>s;
	for(int i=0;i<n;i++)
	{
		dx[i+1]=dx[i];
		dy[i+1]=dy[i];
		if(s[i]=='U') dy[i+1]++;
		if(s[i]=='D') dy[i+1]--;
		if(s[i]=='L') dx[i+1]--;
		if(s[i]=='R') dx[i+1]++;
	}
	ll l=0,r=1e18,ans=-1;
	while(l<=r)
	{
		ll mid=l+r>>1;
		if(valid(mid))
		{
			r=mid-1;
			ans=mid;
		}
		else l=mid+1;
	}
	cout<<ans<<endl;
}

下面的代码是之前没想到二分,蠢蠢的用for循环暴力,真的是脑瓜子没开窍

#include<bits/stdc++.h>
using namespace std;
int a[5];
int main()
{
	int x1,x2,y1,y2,siz;
	cin>>x1>>y1>>x2>>y2;
	cin>>siz;
	int len1=abs(x1-x2)+abs(y1-y2);
	string s;
	cin>>s;
	int row=-1;
	bool flag=1;
	for(int i=0;i<siz;i=(i+1)%siz)
	{
		if(i==0) row++;
		if(row==1)
		{
			int len2=abs(x1-x2)+abs(y1-y2);
			if(len2>len1) 
			{
				flag=0;
				break;
			}
		}
	//	printf("row:%d i:%d\n",row,i);
		if(s[i]=='U') 
		{
			y1++;
			a[1]++;
		}
		if(s[i]=='D') 
		{
			y1--;
			a[2]++;
		}
		if(s[i]=='L') 
		{
			x1--;
			a[3]++;
		}
		if(s[i]=='R') 
		{
			x1++;
			a[4]++; 
		}
		int len=abs(x1-x2)+abs(y1-y2);
	//	printf("x:%d y:%d \n",x1,y1);
	//	printf("第%d秒\n",i+row*siz+1);
		if(len<=i+row*siz+1)
		{
		//	printf("x:%d y1:%d\n",x1,y1);
			printf("%d\n",i+row*siz+1);
			break;
		}
	}
	if(!flag) printf("-1\n");
}

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/88188695

相关文章