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 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小于答案。
曼哈顿距离就是好,两点间的最短路径固定,路线可以再两点画出的矩形内扭动。
源代码
#include<stdio.h>
#include<algorithm> long long sx,sy,tx,ty,dx[],dy[];
int n;
char s[]; int main()
{
scanf("%lld%lld%lld%lld%lld",&sx,&sy,&tx,&ty,&n);
scanf("%s",s+);
for(int i=;i<=n;i++)
{
dx[i]=dx[i-];dy[i]=dy[i-];
if(s[i]=='U') dy[i]++;
else if(s[i]=='D') dy[i]--;
else if(s[i]=='L') dx[i]--;
else dx[i]++;
}
long long dd=std::abs(dx[n])+std::abs(dy[n]);
long long l=,r=1LL<<;
while(l<r)
{
long long mid=l+r>>;
long long turn=mid/n;//轮数
long long rest=mid-turn*n;//零碎
long long x=sx+turn*dx[n]+dx[rest],y=sy+turn*dy[n]+dy[rest];
if(std::abs(x-tx)+std::abs(y-ty)<=mid)
r=mid;
else
l=mid+;
}
if(l==1LL<<) printf("-1\n");
else printf("%lld\n",l);
return ;
}
Educational Codeforces Round 60 (Rated for Div. 2) 即Codeforces Round 1117 C题 Magic Ship的更多相关文章
- 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 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
- Educational Codeforces Round 60 (Rated for Div. 2)
A. Best Subsegment 题意 找 连续区间的平均值 满足最大情况下的最长长度 思路:就是看有几个连续的最大值 #include<bits/stdc++.h> using n ...
- Educational Codeforces Round 60 (Rated for Div. 2)D(思维,DP,快速幂)
#include <bits/stdc++.h>using namespace std;const long long mod = 1e9+7;unordered_map<long ...
- Educational Codeforces Round 60 (Rated for Div. 2)E(思维,哈希,字符串,交互)
#include <bits/stdc++.h>using namespace std;int main(){ string t; cin>>t; int n=t.size() ...
- Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)
题目传送门 题意: 一个魔法水晶可以分裂成m个水晶,求放满n个水晶的方案数(mol1e9+7) 思路: 线性dp,dp[i]=dp[i]+dp[i-m]; 由于n到1e18,所以要用到矩阵快速幂优化 ...
- Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String
题目大意:这是一道交互题.给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3 ...
- Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students 水题
A. Two Rival Students There are
随机推荐
- iOS 关于文件操作 NSFileManager
创建文件夹 + (BOOL)creatDir:(NSString *)path { if (path.length==0) { return NO; } NSFileManager *fileMana ...
- 生成自签名ca 证书 使nginx 支持https
创建服务器私钥,命令会让你输入一个口令:$ openssl genrsa -des3 -out server.key 1024创建签名请求的证书(CSR):$ openssl req -new -ke ...
- 计算几何值平面扫面poj2932 Coneology
Coneology Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4097 Accepted: 859 Descript ...
- ACM_蛇形矩阵
蛇行矩阵 Time Limit: 4000/2000ms (Java/Others) Problem Description: 蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形. Input: ...
- 02—IOC实现项目中的解耦
- VMware workstation 14 CentOs 7.5.1804 虚拟机网卡设置为NAT模式并设置固定IP
一.背景知识 虚拟机网络模式 无论是vmware workstation,virtual box,virtual pc等虚拟机软件,一般来说,虚拟机有三种网络模式: 1.桥接 2.NAT 3. ...
- Linux系统资源监控--linux命令、nmon和spotlight
前言: 系统资源监控一般监控系统的CPU,内存,磁盘和网络.系统分为windows和Linux.本篇主要记录Linux. Linux系统资源监控常用命令及工具 一.常用命令:top.free.iost ...
- Java 基础入门随笔(8) JavaSE版——静态static
面向对象(2) this:代表对象.代表哪个对象呢?当前对象. 当成员变量和局部变量重名,可以用关键字this来区分. this就是所在函数所属对象的引用.(简单说:哪个对象调用了this所在的函数, ...
- C#压缩文件夹至zip,不包含所选文件夹【转+修改】
转自园友:jimcsharp的博文C#实现Zip压缩解压实例[转] 在此基础上,对其中的压缩文件夹方法略作修正,并增加是否对父文件夹进行压缩的方法.(因为笔者有只压缩文件夹下的所有文件,却不想将选中的 ...
- Java线程的sleep方法
sleep方法的签名: public static void sleep (long millis) sleep方法是Thread类的一个方法,作用是:在指定的毫秒内让正在执行的线程休眠(暂停执行) ...