博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces 1117C Magic Ship
阅读量:4473 次
发布时间:2019-06-08

本文共 4293 字,大约阅读时间需要 14 分钟。

time limit per test 2 second

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 Uand 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
0 0
4 6
3
UUU
output
5
input
0 3
0 0
3
UDD
output
3
input
Copy
0 0
0 1
1
L
output
-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).

 

题目大意

  一艘船,在$(sx,sy)$,要到$(tx,ty)$,船长需要考虑风向,每天刮一种方向的风,风向由字符串给出,第一天第一个字符,第二天第二个字符……第$n$天第$n$个字符,第$n+1$天回到第一个字符,即循环节长度$n$天,每天风会把船向下风方向吹开1单位长度,船上发动机可以把船向任意方向移动1单位长度(也可以选择不移动),问最少几天到达目的地。

解题思路

  Neil看数据范围就猜到了二分答案,学长们也是脱口而出二分…菜是原罪…二分最终答案,那么最终答案就是由两个部分组成——几个完整循环节和最后的不到一个循环节的零碎部分。我们让船随风飘mid天,然后看看mid天以后距离终点有多远,如果这个距离小于等于mid,那么可以在这mid天内通过发动机补足这段距离,即mid大于等于答案,否则发动机每天都运转也补足不了,即mid小于答案。

  曼哈顿距离就是好,两点间的最短路径固定,路线可以再两点画出的矩形内扭动。

源代码

1 #include
2 #include
3 4 long long sx,sy,tx,ty,dx[100010],dy[100010]; 5 int n; 6 char s[100010]; 7 8 9 int main()10 {11 scanf("%lld%lld%lld%lld%lld",&sx,&sy,&tx,&ty,&n);12 scanf("%s",s+1);13 for(int i=1;i<=n;i++)14 {15 dx[i]=dx[i-1];dy[i]=dy[i-1];16 if(s[i]=='U') dy[i]++;17 else if(s[i]=='D') dy[i]--;18 else if(s[i]=='L') dx[i]--;19 else dx[i]++;20 }21 long long dd=std::abs(dx[n])+std::abs(dy[n]);22 long long l=1,r=1LL<<62;23 while(l
>1;26 long long turn=mid/n;//轮数27 long long rest=mid-turn*n;//零碎 28 long long x=sx+turn*dx[n]+dx[rest],y=sy+turn*dy[n]+dy[rest];29 if(std::abs(x-tx)+std::abs(y-ty)<=mid)30 r=mid;31 else32 l=mid+1;33 }34 if(l==1LL<<62) printf("-1\n");35 else printf("%lld\n",l);36 return 0;37 }

 

转载于:https://www.cnblogs.com/wawcac-blog/p/10403837.html

你可能感兴趣的文章
一些有用的资源分享(工具+电子书)
查看>>
虚拟现实-ar one
查看>>
python接口自动化测试二十五:执行所有用例,并生成HTML测试报告
查看>>
c# 指定的存储区提供程序在配置中找不到,或者无效
查看>>
最简陋的python数据
查看>>
第一堂java web课
查看>>
操作系统简介
查看>>
第1周小组博客作业--1703班06组
查看>>
vue项目中icon图标的完美引入
查看>>
C语言指针
查看>>
Java的安装
查看>>
0920 JSON数据 蓝懿
查看>>
Azure Cosmos DB 使用费用参考
查看>>
【嵌入式开发】写入开发板Linux系统-模型S3C6410
查看>>
C# 子线程与主线程通讯方法一
查看>>
006——修改tomacat的编码
查看>>
《C程序设计语言》笔记 (八) UNIX系统接口
查看>>
git常用命令
查看>>
Android必知必会-获取视频文件的截图、缩略图
查看>>
(转)理解Bitblt、StretchBlt与SetDIBitsToDevice、StretchDibits
查看>>