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个点的最大入度数,要求添加边构成 ...
随机推荐
- [技巧篇]13.从今天开始做一个有理想的人,放弃alter的调试,拥抱console.log
在js前端开发时,为了调试经常会加上 console.log.但是在有的浏览器(比如IE)中会报错,怎么办呢?好像10之后也开始支持了!如果以防方一,可以使用如下方式 在js文件最前面添加如下js代码 ...
- mysql连接时权限问题 用户问题
启动工程时会连接mysql数据库,此时报错: ERROR 1044 (42000): Access denied for user 'pay'@'localhost' to database 'pay ...
- PHP练习4 留言板
一.要求 二.示例页面 三.网页代码及网页显示 1.denglu.php 登录页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- Vue.js最佳实践(五招让你成为Vue.js大师)
对大部分人来说,掌握Vue.js基本的几个API后就已经能够正常地开发前端网站.但如果你想更加高效地使用Vue来开发,成为Vue.js大师,那下面我要传授的这五招你一定得认真学习一下了. 第一招:化繁 ...
- kndo grid:通过checkbox 实现多选和全选
在kendo grid 里要想通过checkbox 实现多选和权限,我们就要通过templeate 和input 标签对kendo grid 进行自定义 1. 在column 里面加入一列checkb ...
- Python脚本 - 查询磁盘的读写次数信息
测试系统为:Centos 6.7 Python版本为: 3.6.4 脚本功能:查看指定磁盘的读写及时间等相关信息 #!/usr/bin/env python3 from collections imp ...
- vue基本介绍
https://cn.vuejs.org/v2/guide/ Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上 ...
- [Linux]Linux printf 输出重定向【转】
转自:http://www.cnblogs.com/aaronLinux/p/6765145.html?utm_source=itdadao&utm_medium=referral 方法一 # ...
- Linux内核通知链机制的原理及实现【转】
转自:http://www.cnblogs.com/armlinux/archive/2011/11/11/2396781.html 一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其 ...
- 神奇JavaScript框架---Top5
前言 个人观点,供您参考 观点源自作者的使用经验和日常研究 排名基于框架的受欢迎度, 语法结构, 易用性等特性 希望大家能够基于此视频找到最适合自己的框架 下面介绍的都是严格的前端框架和库 前言 To ...