Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))
2 seconds
256 megabytes
standard input
standard output
Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently displaying the contents of page xx, and Vasya wants to read the page yy. There are two buttons on the book which allow Vasya to scroll dd pages forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of 1010 pages, and d=3d=3, then from the first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page — to the first or to the fifth; from the sixth page — to the third or to the ninth; from the eighth — to the fifth or to the tenth.
Help Vasya to calculate the minimum number of times he needs to press a button to move to page yy.
The first line contains one integer tt (1≤t≤1031≤t≤103) — the number of testcases.
Each testcase is denoted by a line containing four integers nn, xx, yy, dd (1≤n,d≤1091≤n,d≤109, 1≤x,y≤n1≤x,y≤n) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.
Print one line for each test.
If Vasya can move from page xx to page yy, print the minimum number of times he needs to press a button to do it. Otherwise print −1−1.
3
10 4 5 2
5 1 3 4
20 4 19 3
4
-1
5
In the first test case the optimal sequence is: 4→2→1→3→54→2→1→3→5.
In the second test case it is possible to get to pages 11 and 55.
In the third test case the optimal sequence is: 4→7→10→13→16→194→7→10→13→16→19.
翻到头只能从头翻,翻到尾只能从尾倒着翻。
代码:
//A
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int inf=0x3f3f3f3f; int main()
{
int t;
cin>>t;
while(t--){
int n,x,y,d,ans=inf;
cin>>n>>x>>y>>d;
if(abs(y-x)%d==) {ans=min(ans,abs(y-x)/d);}
if((y-)%d==) {ans=min(ans,x/d+(y-)/d+(x%d==?:));}
if((n-y)%d==) {ans=min(ans,(n-x)/d+(n-y)/d+((n-x)%d==?:));}
if(ans==inf) cout<<-<<endl;
else cout<<ans<<endl;
}
}
Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))的更多相关文章
- Educational Codeforces Round 55 (Rated for Div. 2) A - Vasya and Book
传送门 https://www.cnblogs.com/violet-acmer/p/10035971.html 题意: 一本书有n页,每次只能翻 d 页,问从x页到y页需要翻动几次? 注意:往前翻最 ...
- Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))
C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))
D. Maximum Diameter Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces 1082 B. Vova and Trophies-有坑 (Educational Codeforces Round 55 (Rated for Div. 2))
B. Vova and Trophies time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D
http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y or x-& ...
- Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】
传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...
- Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】
传送门:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds ...
- Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency
E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
随机推荐
- MySQL5.5安装图解
MySQL5.5安装图解... ====================== 第一部分:去官网下载MySQL安装包... MySQL下载官网:https://dev.mysql.com/downloa ...
- Object.defineProperty 与 属性描述符
为JavaScript对象新增或者修改属性,有两种不同方式:直接使用=赋值或者使用Object.defineProperty 定义,使用后者的话还可以设置属性的描述符. Object.definePr ...
- 页面自适应<meta name="viewport">标签设置
viewport: 它在页面中设置,是应对手机模式访问网站.网页对屏幕而做的一些设置.通常手机浏览器打开页面后,会把页面放在一个虚拟的“窗 口”–这个比窗口大,也就是你常发现页面可以进行拖动.放大放小 ...
- Tomcat 映射虚拟目录和程序热部署
虚拟目录的设置 方法一:在${tomcat安装目录}/conf/Catalina/localhost目录下创建一个xml文件,任意文件名都可以,但是此文件名是web应用发布后的虚拟目录: 比如创建一个 ...
- C# 后台获取请求来源、文件下载
文件流下载文件 void BigFileDownload() { try { string FileName = "测试.docx"; string filePath = Page ...
- spring事务的一些注意点
参考文章 http://blog.csdn.net/qq_34021712/article/details/75949779 ©王赛超 1.在需要事务管理的地方加@Transactional 注解 ...
- vijos 1464 积木游戏 DP
描述 积木游戏 SERCOI 最近设计了一种积木游戏.每个游戏者有N块编号依次为1 ,2,…,N的长方体积木.对于每块积木,它的三条不同的边分别称为"a边"."b边&qu ...
- android程序员成长路径的思考
我之前就想过要写这个话题,不过之前没有什么认识,我只是在阅读别人的见解,看法.昨天晚上,我阅读了这篇文章<产品经理罗永浩:用户体验探索,没有尽头>,这篇文章描述了罗永浩对锤子手机设计细节的 ...
- 「6月雅礼集训 2017 Day4」暴力大神hxx
[题目大意] 给出一个n重循环,每重循环有范围$[l, r]$,其中$l$,$r$可能是之前的变量,也可能是常数.求循环最底层被执行了多少次. 其中,保证每个循环的$l$,$r$最多有一个是之前的变量 ...
- poj 1298 The Hardest Problem Ever
题目链接:http://poj.org/problem?id=1298 题目大意:按照所给的顺序要求将输入的字符串进行排列. #include <iostream> #include &l ...