1. 程式人生 > >Educational Codeforces Round 60 (Rated for Div. 2) C. Magic Ship

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

st2 mat rmi nts enc switch ret win cati

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;
}

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