当前位置: 代码迷 >> 综合 >> (模拟)A. Vasya and Book—— Educational Codeforces Round 55 (Rated for Div. 2)
  详细解决方案

(模拟)A. Vasya and Book—— Educational Codeforces Round 55 (Rated for Div. 2)

热度:90   发布时间:2023-11-02 19:57:44.0

传送门:A. Vasya and Book

题解:

  • 向左翻d页,如果翻超界,那就是翻到第一页;
  • 向右翻d页,如果翻超界,那就是翻到第n页。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;int main(){int t;scanf("%d",&t);int n,x,y,d;while(t--){scanf("%d%d%d%d",&n,&x,&y,&d);//if(x>y) swap(x,y);  起始页就不一样了int ans,ans1;if(x==y) ans=0;else if(abs(y-x)%d==0){ans=abs(y-x)/d;}else{ans=(x-1+d-1)/d;if((y-1)%d==0){ans+=(y-1)/d;}else ans=INF;ans1=(n-x+d-1)/d;if((n-y)%d==0){ans1+=(n-y)/d;}else ans1=INF;ans=min(ans,ans1);}if(ans==INF) ans=-1;printf("%d\n",ans);}return 0;
}

 

  相关解决方案