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
随机推荐
- 【Learning】多项式的一些东西
FFT 坑 NTT 将\(FFT\)中的单位复数根改成原根即可. 卡常版NTT模版 struct Mul { int Len; int wn[N], Lim; int rev[N]; inline v ...
- SP2916 GSS5 - Can you answer these queries V
给定一个序列.查询左端点在$[x_1, y_1]$之间,且右端点在$[x_2, y_2]$之间的最大子段和,数据保证$x_1\leq x_2,y_1\leq y_2$,但是不保证端点所在的区间不重合 ...
- sql注入方法以及防范
sql注入方法: 1.数字注入 ; get请求 www.bobo.com?id=1 可以查出 ID等于1的一条数据. 如果有人在链接后面增加 www.bobo.com?id=1 or 1=1 / w ...
- fread/fwrite实现复制功能
1. fread/fwrite实现复制功能 #include <stdio.h> #include <stdlib.h> #define BUFFSIZE 4096 //执行 ...
- C#将类对象转换为字典
主要是实现将类里面 的属性和对应的值转换为字典的键和值. public class RDfsedfw { /// <summary> /// 将匿名类转换为字典 /// </summ ...
- CF869C The Intriguing Obsession
思路: 分别在两种不同颜色的岛屿群之间进行搭桥.因为相同颜色的岛屿之间不能有边,任意两个相同颜色的岛屿不能同时和另外一个不同颜色的岛屿都有边.实现: #include <bits/stdc++. ...
- poj1240 Pre-Post-erous!
思路: 根据前序序列和后序序列递归构造m叉树,确定每个节点的子节点数量.再用组合数公式累乘. 实现: #include <iostream> using namespace std; ][ ...
- P1334 瑞瑞的木板
题目描述 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每根的长度为整数Li(1≤Li≤50,000).于是,他神奇地买了一根足够长的木板,长度为 ...
- python 模块-easygui.buttonbox
2018-03-0315:43:11 ): Yes_or_No = easygui.buttonbox("是否良品?", choices=['Yes', 'No', '退出']) ...
- Python学习日记之文件读取操作
Python内置了文件读写的函数open,read 用法示例: open('/home/root/files.txt ') 在打开文件后,操作完成后可以使用close()关闭文件,但比较好的文件读写方 ...