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

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You a captain of a ship. Initially you are standing in a point
(
x
1
,
y
1
)
(x1,y1)
(obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point
(
x
2
,
y
2
)
(x2,y2)
.
You know the weather forecast — the string
s
s
of length
n
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
s
1
s1
, the second day —
s
2
s2
, the
n
n
-th day —
s
n
sn
and
(n+1)
(n+1)
-th day —
s
1
s1
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 U and 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
(
x
2
,
y
2
)
(x2,y2)
.
Input
The first line contains two integers
x
1
,
y
1
x1,y1
(
0≤
x
1
,
y
1

10
9
0≤x1,y1≤109
) — the initial coordinates of the ship.
The second line contains two integers
x
2
,
y
2
x2,y2
(
0≤
x
2
,
y
2

10
9
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
n
(
1≤n≤
10
5
1≤n≤105
) — the length of the string
s
s
.
The fourth line contains the string
s
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
(
x
2
,
y
2
)
(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)
.

题解:这道题可以.用二分来做,好吧,我根本想不到,太强了.

题意是给你起点和终点,还有循环出现的风向.船顺风行驶,走两格;逆风不动;与风成角度,斜着开,船自己不行驶会随风开.

这里的处理方法是:把随风行和船自己行驶分开.用dx[],dy[]计算风向循环节内的随风行的距离;最后对天数进行二分查找,因为不知道到底会行几天,所以我们先把右值先设置的大一点,

当在第x天时,船随风行到达的点与终点的曼哈顿距离(|x1-x2|+|y1-y2|)少于等于x,说明船时可以在x天内到达,那么r=mid-1;

否则,l=mid+1;

#include <iostream>
#include <cstdio>
#include <cmath>
const int N=1e5+5;
typedef long long ll;
using namespace std;
//char s[N];
int dx[N];
int dy[N];
char s[N];
int x1,yy,x2,y2,n;
bool check(ll x){
    ll cx=x1+x/n*dx[n]+dx[x%n];
    ll cy=yy+x/n*dy[n]+dy[x%n];
    if((abs(x2-cx)+abs(y2-cy))<=x) return true;
    return false;
}
int main()
{

    scanf("%d%d%d%d",&x1,&yy,&x2,&y2);
    scanf("%d",&n);
    //cout<<"jjjj"<<endl;
    //dx[0]=x1,dy[0]=yy;
    scanf("%s",s+1);
    for(int i=1;i<=n;i++){
        //scanf("%s",s);
        dx[i]=dx[i-1];dy[i]=dy[i-1];
        //cout<<"fjjjj"<<endl;
        switch(s[i]){
            case 'U':dy[i]++;break;
            case 'D':dy[i]--;break;
            case 'L':dx[i]--;break;
            case 'R':dx[i]++;break;
        }
        //cout<<"jjjj"<<endl;
    }
    //cout<<"hello"<<endl;
    ll l=1,r=1e16;
    ll ans=0;
    while(l<=r){
        ll mid=l+(r-l)/2;
        if(check(mid)){
            r=mid-1;
            ans=mid;
        }
        else l=mid+1;
        //cout<<l<<" "<<r<<endl;
    }
    if(ans==0){
            printf("-1\n");
            return 0;
    }
    printf("%I64d\n",ans);
    //cout << "Hello world!" << endl;
    return 0;
}
上一篇:制作镜像语言网站


下一篇:设计模式之装饰者模式应用案例(一)