zoukankan      html  css  js  c++  java
  • C. Magic Ship cf 二分

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

    思路:

    先对前面n天进行计算用dx,dy数组来记录风让船走的距离。然后进行二分,对这一天设为x进行判断,在第x天船随着风走了一个新的位置,这个位置横纵坐标和终点进行绝对值求和为sum(这个sum可以直接理解为人工操作的步数,也就是天数)

    如果sum<=x,说明x太大了,r=x-1;.... 

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const int maxn=1e5+19;
    char s[maxn];
    ll sx,sy,gx,gy,n;
    ll dx[maxn],dy[maxn];
    
    int check(ll x)
    {
    	ll ex=(x/n)*dx[n]+dx[x%n];
    	ll ey=(x/n)*dy[n]+dy[x%n];
    	
    	if(abs(sx+ex-gx)+abs(sy+ey-gy)<=x) return 1;//船随着风走了这么久之后,如果接下来的路程步数小于x(即人走),那就说明天数过大。 
    	return 0;
    }
    
    int main()
    {
    	scanf("%I64d%I64d",&sx,&sy);
    	scanf("%I64d%I64d",&gx,&gy);
    	scanf("%d%s",&n,s+1);
    	for(int i=1;i<=n;i++)
    	{
    		dx[i]=dx[i-1];
    		dy[i]=dy[i-1];
    		if(s[i]=='U') dy[i]++;
    		if(s[i]=='D') dy[i]--;
    		if(s[i]=='L') dx[i]--;
    		if(s[i]=='R') dx[i]++;
    	}
    	ll l=0,r=1e18,ans=-1;
    	while(r>=l)
    	{
    		ll mid=(l+r)/2;
    		if(check(mid))
    		{
    			r=mid-1;
    			ans=mid;
    		}
    		else l=mid+1;
    	}
    	printf("%I64d
    ",ans);
    	return 0;
    }
    

      

  • 相关阅读:
    洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]
    洛谷P1082 同余方程 [2012NOIP提高组D2T1] [2017年6月计划 数论06]
    洛谷P2667 超级质数 [2017年6月计划 数论05]
    洛谷P1965 转圈游戏 [2013NOIP提高组 D1T1][2017年6月计划 数论04]
    洛谷P1595 信封问题
    洛谷P1062 数列 [2017年6月计划 数论03]
    洛谷P2835 刻录光盘 [2017年6月计划 强连通分量02]
    洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]
    【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线
    【模板】ST表 洛谷P1816 忠诚
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10401409.html
Copyright © 2011-2022 走看看