日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

思维水题

發(fā)布時(shí)間:2023/12/2 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 思维水题 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接:https://codeforces.com/problemset/problem/1082/A

?

?

題目意思:

一本書(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 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.

Input

The first line contains one integer?tt?(1t1031≤t≤103) — the number of testcases.

Each testcase is denoted by a line containing four integers?nn,?xx,?yy,?dd?(1n,d1091≤n,d≤109,?1x,yn1≤x,y≤n) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.

Output

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.

Example input Copy 3 10 4 5 2 5 1 3 4 20 4 19 3 output Copy 4 -1 5 Note

In the first test case the optimal sequence is:?421354→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:?47101316194→7→10→13→16→19.

?

轉(zhuǎn)載于:https://www.cnblogs.com/q1204675546/p/10060549.html

總結(jié)

以上是生活随笔為你收集整理的思维水题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。