<Codeforce>1082A. Vasya and Book
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.
INPUT:
3
10 4 5 2
5 1 3 4
20 4 19 3
OUTPUT:
4
-1
5
记得fmax和fmin要开G++编译器!
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int t,p,a,b,x;
cin>>t;
while(t--){
long long sum=0;
cin>>p>>a>>b>>x;
if(a==b){
cout<<0<<endl;
}else{
int k=abs(a-b); if(k%x==0){
sum=k/x;
cout<<sum<<endl;
}else{
/*两种情况,a->1->b,a->p->b*/
int m1,m2;
if((a-1)%x==0){
sum+=(a-1)/x;
}else{
sum+=(a-1)/x+1;
}
if((b-1)%x==0){
sum+=(b-1)/x;
}else{
sum=-1;
}
m1=sum;
sum=0;
if((p-a)%x==0){
sum+=(p-a)/x;
}else{
sum+=(p-a)/x+1;
}
if((p-b)%x==0){
sum+=(p-b)/x;
}else{
sum=-1;
}
m2=sum;
//cout<<m1<<endl<<m2<<endl<<endl;
if(m1!=-1&&m2!=-1){
if(m1<m2){
cout<<m1<<endl;
}else{
cout<<m2<<endl;
}
//cout<<fmin(m1,m2)<<endl;
}if(m1!=-1&&m2==-1){
cout<<m1<<endl;
}if(m1==-1&&m2!=-1){
cout<<m2<<endl;
}if(m1==-1&&m2==-1){
cout<<-1<<endl;
}
}
}
}
return 0;
}
<Codeforce>1082A. Vasya and Book的更多相关文章
- CodeForce Educational round Div2 C - Vasya and Robot
http://codeforces.com/contest/1073/problem/C 题意:给你长度为n的字符串,每个字符为L, R, U, D.给你终点位置(x, y).你每次出发的起点为( ...
- Milliard Vasya's Function-Ural1353动态规划
Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning mathematician. He decided to make ...
- Codeforce 493c
H - Vasya and Basketball Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- CF460 A. Vasya and Socks
A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
- Codeforces Round #281 (Div. 2) D. Vasya and Chess 水
D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- 闲鱼Flutter&FaaS云端一体化架构
讲师介绍 国有,闲鱼架构团队负责人.在7月13号落幕的2019年Archsummit峰会上就近一年来闲鱼在Flutter&FaaS一体化项目上的探索和实践进行了分享. 传统Native+Web ...
- poj1637&&hdu1956 混合欧拉回图判断
欧拉路:经过所有路有且仅有1次,可以路过所有的点. 无向图: 图连通,所有点都是偶数度,或者只有两个点是奇数度.当所有点是偶数度时欧拉路起点可以是任意点:当有两个奇数度点时起点必须是奇数度点. 有向 ...
- UVA_445:Marvelous Mazes
Language:C++ 4.8.2 #include<stdio.h> #include<string.h> #include<ctype.h> int main ...
- Ubuntu修改root密码,ssh 允许root用户登录
1,切换为root用户 2,passwd root(or others) 3,输两次密码 4,重启. ssh允许root用户登录: 1,vim /etc/ssh/sshd_config 2,修改Per ...
- nodeJs学习-01 http模块
http模块基础: const http = require("http"); //引入http系统模块 var server = http.createServer(functi ...
- vue单页面项目返回上一页无效,链接变化了,但是页面没有变化
在最近的项目中,返回上一页没有效果,经过好久的排查才发现问题,是路由守卫写法不规范导致. 在项目中用路由守卫做了登录拦截,没登录的跳转到登录页面.页面跳转和拦截都没问题,但是返回上一页就不行了,也没有 ...
- tensorflow兼容处理 tensorflow.compat.v1
https://www.wandouip.com/t5i183316/ 引言 原来引用过一个段子,这里还要再引用一次.是关于苹果的.大意是,苹果发布了新的开发语言Swift,有非常多优秀的特征,于是很 ...
- python if 选择结构
- javascript、jquery、AJAX总结 标签: javascriptjqueryajax 2016-01-23 10:25 2415人阅读
其实在学习之前,就已经用上了js,jquery和ajax,不过当时不清楚这些的区别,就全都当成js来看,然后别人一说jquery,ajax都觉得好像很高级,等到自己学习的时候,倒是对这些更清楚了一点, ...
- C++讲课总结 标签: c++总结 2015-02-28 14:48 671人阅读 评论(25) 收藏
昨天老师算是给串了一本C++ 的课本,根据自己的理解,赶紧记录一下,也好作为自己学习时候的根据. C++编程简介:每本讲语言的书,第一章总是简介,内容无非是发展历史,语言特色等东西,专业的东西不多,都 ...