<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 ...
随机推荐
- 洛谷 P1505 [国家集训队]旅游 树链剖分
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例: 输出样例: 说明 思路 AC代码 总结 题面 题目链接 P1505 [国家集训队]旅游 题目描述 Ray 乐 ...
- 程序跳转到itunes商店
找到应用程序,点击应用程序下面的小三角图标,再选择"复制链接",就可以获取此应用的链接了. 比如: itunes.apple.com/cn/app/bai-du-wen-kuhd/ ...
- Selenium-----wait的三种等待
在UI自动化测试中,必然会遇到环境不稳定,网络慢的情况,这时如果你不做任何处理的话,代码会由于没有找到元素,而报错.这时我们就要用到wait(等待),而在Selenium中,我们可以用到一共三种等待, ...
- springboot对shiro进行mock单元测试
环境:junit-5.Spring5.0.x.Spring Boot 2.0.x 以下是用来权限测试的接口: @ApiOperation("[可接入]分页查询管理员") @ApiR ...
- HZOJ trade
强烈谴责$skyh$的没$\Huge 脸$行为. 很经典的可反悔贪心,然而我一直以为是sbdp还一直想着怎么优化…… 正常的贪心肯定是不对的. 但是由于A-C=A-B+B-C, 所以用一个小根堆维护, ...
- Uva 568 【大整数】
UVa568 题意:求N!(N<=10000)的最后一位非0数. 10000以内5^5 = 3125最多可以影响后5位.所以直接保存后五位就行. #include<iostream> ...
- Java练习 SDUT-1580_闰年
闰年 Time Limit: 1000 ms Memory Limit: 32768 KiB Problem Description 时间过得真快啊,又要过年了,同时,我们的人生也增长了一年的阅历,又 ...
- 『创造 Cloud Toolkit』贡献排行榜——如何参与定义一款 IDE 插件?
自从我们团队在去年12月发布 Cloud Toolkit(一款让开发部署效率提速 8 倍的 IDE 插件)以来,已帮助数以万计的开发者们提高了云上的部署效率,期间,开发者们不仅积极地向 Cloud T ...
- Flask学习之三 web表单
本部分Miguel Grinberg教程的翻译地址:http://www.pythondoc.com/flask-mega-tutorial/webforms.html 开源中国的:http://ww ...
- eBPF Tracing 入门教程与实例
原文链接 Learn eBPF Tracing: Tutorial and Examples译者 弃余 在 LPC'18(Linux Plumber's conference) 会议上,至少有24个关 ...