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,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 (x2,y2)(x2,y2) .

Input

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

The second line contains two integers x2,y2x2,y2 (0≤x2,y2≤1090≤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 (1≤n≤1051≤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\n",ans);
return 0;
}

  

C. Magic Ship cf 二分的更多相关文章

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

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  2. CF1117C Magic Ship

    CF1117C Magic Ship 考虑到答案具单调性(若第 \(i\) 天能到达目的点,第 \(i+1\) 天只需向风向相反的方向航行),可以二分答案. 现在要考虑给出一个天数 \(m\) ,问 ...

  3. 题解-Magic Ship

    Magic Ship 你在 \((x_1,y_1)\),要到点 \((x_2,y_2)\).风向周期为 \(n\),一个字符串 \(s\{n\}\) 表示风向(每轮上下左右),每轮你都会被风向吹走一格 ...

  4. CodeForces 1117C Magic Ship (循环节+二分答案)

    <题目链接> 题目大意: 给定起点和终点,某艘船想从起点走到终点,但是海面上会周期性的刮风,船在任何时候都能够向四个方向走,或者选择不走,船的真正行走路线是船的行走和风的走向叠加的,求船从 ...

  5. C. Magic Ship (思维+二分)

    https://codeforces.com/contest/1117/problem/C 你是一个船长.最初你在点 (x1,y1) (显然,大海上的所有点都可以用平面直角坐标描述),你想去点 (x2 ...

  6. Codeforces 1117C Magic Ship (二分)

    题意: 船在一个坐标,目的地在一个坐标,每天会有一个风向将船刮一个单位,船也可以移动一个单位或不动,问最少几天可以到目的地 思路: 二分天数,对于第k天 可以分解成船先被吹了k天,到达坐标(x1+su ...

  7. codeforces 350 div2 D Magic Powder - 2 二分

    D2. Magic Powder - 2 time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分

    D1. Magic Powder - 1 题目连接: http://www.codeforces.com/contest/670/problem/D1 Description This problem ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) 即Codeforces Round 1117 C题 Magic Ship

    time limit per test 2 second memory limit per test 256 megabytes input standard inputoutput standard ...

随机推荐

  1. mybatis_04 resultType和resultMap区别

    resultType 使用resultType进行结果映射时,查询的列名和映射的pojo属性名完全一致,该列才能映射成功. 如果查询的列名和映射的pojo属性名全部不一致,则不会创建pojo对象: 如 ...

  2. 解决org.hibernate.exception.SQLGrammarException:could not insert

    今天在使用hibernate搭建项目时碰到了这个错误,找了好半天没能发现错误的原因, 上网求助了一下 发现出现这个bug的原因是因为数据表使用了数据库的关键字(保留字) 然后检查了一下,发现字段名称都 ...

  3. vue(三)-父子组件通信

    原因 :  Vue 的组件作用域都是孤立的,不允许在子组件的模板内直接引用父组件的数据.必须使用特定的方法才能实现组件之间的数据传递. props  父组件给子组件传递数据 props:作用是父组件给 ...

  4. MVC模式-----struts2框架

    MVC模式-----struts2框架 一.struts MVC模式 1.概述 MVC是模型(model)---视图model(view)---控制器(control)的缩写,是一种用于将逻辑.数据和 ...

  5. K8S 调度器,预选策略,优选函数

    Kubernetes Scheduler 提供的调度流程分三步: 预选策略(predicate) 遍历nodelist,选择出符合要求的候选节点,Kubernetes内置了多种预选规则供用户选择. 优 ...

  6. Mybatis 同时传入多个参数和对象

    流程 1,mapper 接口文件使用 @param 注解(一个参数就不用使用注解,多个参数要么使用注解,要么使用数组的方式取值) 2,mapper xml 文件使用 mapper 接口文件传参 pub ...

  7. ViewPager结合view无限滑动

    使用viewPager进无限滑动,这里的实现是在适配器里面进行,当然在外头使用滑动监听也行. import android.support.v4.view.PagerAdapter; import a ...

  8. LockSupport的源码实现原理以及应用

    一.为什么使用LockSupport类 如果只是LockSupport在使用起来比Object的wait/notify简单, 那还真没必要专门讲解下LockSupport.最主要的是灵活性. 上边的例 ...

  9. Android为TV端助力 不需要Socket的跨进程推送消息AIDL!

    上篇介绍了跨进程实时通讯http://www.cnblogs.com/xiaoxiaing/p/5818161.html 但是他有个缺点就是服务端无法推送消息给客户端,今天这篇文章主要说的就是服务器推 ...

  10. vue 构建项目 文件引入

    1.vue引用依赖文件.  举例:axios 先安装 axios.如果直接安装 vue-axios 会报错 npm install axios npm install --save axios vue ...