思维水题
?
?
題目意思:
一本書(shū)有1到n頁(yè),起始頁(yè)數(shù)是x,
一次翻頁(yè)只能翻d(向前或者向后翻,但是不能翻出去,例如n=5,x=1? y=5,d=10? ? ?)? ?這種情況下是可以翻頁(yè)到5的。
問(wèn)能否翻y頁(yè)。否輸出-1,能則輸出從x到y(tǒng)所需的最少翻頁(yè)次數(shù)
?
?
思路:
1. x能直接到y(tǒng)。
2. 起點(diǎn)x非1且非n,同時(shí)d>=n。
3. x先到1,再到y(tǒng)。
4. x先到n,再到y(tǒng)?!?/strong>
?
AC代碼如下:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cstdlib> #include<cmath> #include<algorithm> #include<vector> #include<queue> #include<stack> #include<set> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define Swap(a,b,t) t=a,a=b,b=t #define Mem0(x) memset(x,0,sizeof(x)) #define Mem1(x) memset(x,-1,sizeof(x)) #define MemX(x) memset(x,0x3f,sizeof(x)) using namespace std; typedef long long ll; const ll inf=0xfffffffff; const double eps=1e-12; int main() {int t;cin>>t;while(t--){ll n,x,y,d;cin>>n>>x>>y>>d;if (x==y){printf("0\n");continue;}else if (abs(x-y)%d==0){printf("%d\n",abs(x-y)/d);continue;}else if ((y!=1&&y!=n)&&d>=n){printf("-1\n");continue;}else{ //x->1->yll ans1=0,ans2=0,ans;ans1=ans1+(x-1)/d;if ((x-1)%d!=0){ans1+=1;}if ((y-1)%d!=0)ans1=inf;else ans1=ans1+(y-1)/d;//x->n->1ans2=ans2+(n-x)/d;if ((n-x)%d!=0)ans2++;if((n-y)%d!=0)ans2=inf;elseans2=ans2+(n-y)/d; // printf("%lld\n\n",ans2);ans=min(ans1,ans2);if (ans==inf)printf("-1\n");else printf("%lld\n",ans); // printf("inf=%lld",inf); }} return 0; }?
A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputVasya 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.
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≤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.
OutputPrint 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.
Example input Copy 3 10 4 5 2 5 1 3 4 20 4 19 3 output Copy 4 -1 5 NoteIn 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.
?
轉(zhuǎn)載于:https://www.cnblogs.com/q1204675546/p/10060549.html
總結(jié)
- 上一篇: 嘉庆帝的下一代谁继位(嘉庆帝最喜欢的是哪
- 下一篇: 制造业物料清单BOM、智能文档阅读、科学