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

题目链接:http://codeforces.com/contest/1082/problem/A

题意:一本书有n页,现在让你从 x 页,翻到 y 页,每次只可以翻 t 页。求最少多少次可以翻到y页。

题解:判断以下情况即可:

1,若能直接从 x 翻到 y,那么这个就是最小次数。

2,若不能,则先从 x 到 1 ,然后从 1 到 y。或者,先从x到 n ,然后从 n 到 1. 两种求最小。

若不能到达 则输出 -1.

代码如下:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e6+5;
int a[maxn];
int x,y,n,d;

int main(){
	int z;
	scanf("%d",&z);
	while(z--){
		scanf("%d%d%d%d",&n,&x,&y,&d);
		if(abs(y-x) % d == 0){
			printf("%d\n",abs(y-x)/d);
		}
		else{
			int c = 2e9;
			if((y-1)%d == 0){
				c = x/d+(y-1)/d;
				if(x%d != 0)
					c++;
			}
			
			int e = 2e9;
				
			if((n - y) % d == 0){
			
				e = (n-x)/d + (n - y)/d;
				if((n-x)%d != 0)
				e++;
			}
			
			if(c == 2e9 && e == 2e9)
				printf("-1\n");
			else
				printf("%d\n",min(c,e));
				
		}
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值