<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
Help Vasya to calculate the minimum number of times he needs to press a button to move to page yy.
InputThe 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≤109
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
以後我再用fmin再不開G++我是DOG!(無奈自閉了。。)
#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