C. Magic Ship Educational Codeforces Round 60 (Rated for Div. 2)

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,y1)(x,y−1);
  • if wind blows the direction L, then the ship moves from (x,y)(x,y) to (x1,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 U and the ship moves the direction L, then from point (x,y)(x,y) it will move to the point (x1,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 (0x1,y11090≤x1,y1≤109) — the initial coordinates of the ship.

The second line contains two integers x2,y2x2,y2 (0x2,y21090≤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 (1n1051≤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天能否到达目的地,则维护一个前缀和,找循环节乱搞一下就过了....

#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
ll X,Y,x,y,n;
pair<ll,ll> sum[maxn];
bool check(ll mid)
{
    ll val=mid/n;
    ll lef=mid%n;
    ll dx=X-x,dy=Y-y;
    dx-=val*sum[n-1].first;
    dy-=val*sum[n-1].second;
    dx-=sum[lef-1].first;
    dy-=sum[lef-1].second;
    ll temp=abs(dx)+abs(dy);
    if(temp<=mid) return true;
    else return false;
}
int main()
{
    string s;
    cin>>x>>y>>X>>Y>>n>>s;
    for(int i=0;i<n;++i)
    {
      if(s[i]=='U')
      {
          sum[i].first=sum[i-1].first;
          sum[i].second=sum[i-1].second+1;
      }
      else if(s[i]=='D')
      {
          sum[i].first=sum[i-1].first;
          sum[i].second=sum[i-1].second-1;
      }
      else if(s[i]=='L')
      {

          sum[i].first=sum[i-1].first-1;
          sum[i].second=sum[i-1].second;
      }
      else
      {

          sum[i].first=sum[i-1].first+1;
          sum[i].second=sum[i-1].second;
      }
    }
    ll L=1,R=2e18;
    ll res=-1;
    while(L<=R)
    {
        ll mid=(L+R)>>1;
        if(check(mid))
        {
            res=mid;
            R=mid-1;
        }
        else L=mid+1;
    }
    cout<<res<<endl;
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/zyf3855923/p/10401070.html