Codeforces Contest 1117 problem C Magic Ship——二分

You a captain of a ship. Initially you are standing in a point (x1,y1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (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 s1, the second day — s2, the n-th day — sn and (n+1)-th day — s1 again and so on.

Ship coordinates change the following way:

if wind blows the direction U, then the ship moves from (x,y) to (x,y+1);
if wind blows the direction D, then the ship moves from (x,y) to (x,y−1);
if wind blows the direction L, then the ship moves from (x,y) to (x−1,y);
if wind blows the direction R, then the ship moves from (x,y) to (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) it will move to the point (x−1,y+1), and if it goes the direction U, then it will move to the point (x,y+2).

You task is to determine the minimal number of days required for the ship to reach the point (x2,y2).

Input
The first line contains two integers x1,y1 (0≤x1,y1≤109) — the initial coordinates of the ship.

The second line contains two integers x2,y2 (0≤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≤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 (x2,y2).

If it’s impossible then print “-1”.

Examples
inputCopy
0 0
4 6
3
UUU
outputCopy
5
inputCopy
0 3
0 0
3
UDD
outputCopy
3
inputCopy
0 0
0 1
1
L
outputCopy
-1
Note
In the first example the ship should perform the following sequence of moves: “RRRRU”. Then its coordinates will change accordingly: (0,0) → (1,1) → (2,2) → (3,3) → (4,4) → (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,1) → (0,0).

In the third example the ship can never reach the point (0,1).

题意:

有一艘小船,它受着风浪的影响,风往U吹,它会往上走一格,与此类似走左右,下。同时它自己本身可以往任意方向划一格,与风叠加。问你从一个点走到另一个点的最小时间是多少,不可能输出-1.

题解:

模拟了半天,写不下去了看了别人的代码才知道是二分,稍微想一下就知道确实满足二分的条件,因为它本身可以走一格,所以不可能离目标点越来越远,所以是单调非递增的一个函数,那么二分就好了。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
char s[N];
ll dis(ll x1,ll y1,ll x2,ll y2)
{
    return abs(x1-x2)+abs(y1-y2);
}
int main()
{
    int x1,x2,y1,y2;
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    int n;
    scanf("%d%s",&n,s);
    ll low=0,high=1e18,ans=-1;
    ll up=0,down=0,le=0,rig=0,posx=0,posy=0;
    for(int i=0;i<n;i++)
        if(s[i]=='U')
            up++,posy++;
        else if(s[i]=='R')
            rig++,posx++;
        else if(s[i]=='L')
            le++,posx--;
        else
            down++,posy--;
    while(high>=low)
    {
        ll mid=(low+high)/2;
        ll t=mid/n;
        ll nx=x1+posx*t,ny=y1+posy*t;
        ll pos=mid%n;
        for(int i=0;i<pos;i++)
            if(s[i]=='U')
                up++,ny++;
            else if(s[i]=='R')
                rig++,nx++;
            else if(s[i]=='L')
                le++,nx--;
            else
                down++,ny--;
        if(dis(nx,ny,x2,y2)<=mid)
            high=mid-1,ans=mid;
        else
            low=mid+1;
    }
    printf("%lld\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值