题意:当前在看书的第 x 页,每次可以向前或者向后翻 d 页,这个书一共 n 页,问能否用最小操作翻到第 y 页。

题解:三种情况:1、直接翻能到的一定最短。 2、先翻到第一页,然后往后翻,翻到第 y 页。3、先翻到第 n 页,然后往前翻,翻到第 y 页。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; int main()
{
int ans,t,n,x,y,d;
cin >> t;
while(t--)
{
ans = 0;
cin >> n >> x >> y >> d;
if(abs(x-y) %d == 0)
{
ans = abs(x-y) / d;
printf("%d\n",ans);
continue;
}
else {
int x1;
if(x % d == 0) x1 = x /d;
else x1 = x / d +1;
if((y-1) % d == 0)
{
x1 += (y-1) / d;
}
else x1 = -1;
int x2;
if(abs(n - x) % d == 0) x2 = abs(n - x) / d;
else x2 = abs(n - x) / d + 1;
if((n-y)%d==0)
{
x2 += (n-y)/d;
}
else x2 = -1;
if(x1 == -1 && x2 == -1) ans = -1;
else if(x1 == -1 && x2 >= 0) ans = x2;
else if(x2 == -1 && x1 >= 0) ans = x1;
else ans = min(x1,x2);
printf("%d\n",ans);
}
} return 0;
}

A. Vasya and Book ( Codeforces Educational Codeforces Round 55 )的更多相关文章

  1. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  2. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  3. Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

    A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. D. Vasya And The Matrix(Educational Codeforces Round 48)

    D. Vasya And The Matrix time limit per test2 seconds memory limit per test256 megabytes inputstandar ...

  5. codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers

    题目链接:http://codeforces.com/problemset/problem/616/A 题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小 模拟即可.提供两个版本,数组 ...

  6. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  7. Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph

    E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...

  8. Codeforces Educational Codeforces Round 15 D. Road to Post Office

    D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. Codeforces Educational Codeforces Round 15 C. Cellular Network

    C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. kubernetes第八章--NFS PersistentVolume

  2. mac 上使用 idea 上传项目代码到阿里云git上

    1.Idea 打开需要上传的项目   2.先在本地创建一个git仓库 VCS --> Import into Version Control --> Create Git reposito ...

  3. Linux下使用nextcloud搭建个人网盘

    市面上有那么多的网盘服务提供商,为什么还要自己搭建网盘呢?主要有以下原因: 免费的网盘都有种种限制,要么不限速容量小(onedriver,google driver),要么容量大限速(百度云) 付费网 ...

  4. centOS学习part5:oracle 11g安装之环境准备

    0 前几篇依次向大家介绍了centOS的基本安装以及常用软件的安装配置,接下来我们将挑战的是oracle 11g的安装配置.与之前安装的软件不一样的是,由于oracle并非开源免费软件(需要向orac ...

  5. iview Carousel 轮播图自适应宽高;iview 轮播图 图片重叠问题;iview tabs 高度互相影响问题;vue this问题;

    最终效果图: 一.轮播图中图片自适应宽高:  <Carousel loop v-bind:height="imgHeight+'px'" v-model="caro ...

  6. 过滤器+用session验证是否登陆过

    过滤器: public class MyActionFilter : ActionFilterAttribute//继承ActionFilterAttribute类 { public override ...

  7. CSS3 颜色渐变、阴影、渐变的阴影

    css阴影: 外阴影:box-shadow:X Y Npx #color; 内阴影:box-shadow:inset X Y Npx #color; 文字阴影:text-shadow:X Y Npx ...

  8. AD如何改变PCB文件的黑色背景

    第一步:打开AD软件,新建一个PCB文件.   2 第二步:在黑色区域随便画一个封闭的多边形——注意一定要是封闭的!   3 第三步:点击“Ctrl + A”快捷键将PCB整个文件选中.   4 第四 ...

  9. 私有容器镜像仓库harbor

    私有镜像仓库Harbor 1.Harbor概述 Habor是由VMWare公司开源的容器镜像仓库.事实上,Habor是在Docker Registry上进行了相应的企业级扩展,从而获得了更加广泛的应用 ...

  10. MySQL数据库机房裁撤问题总结

    背景:公司某一机房需要裁撤,涉及到大量DB服务器,需要在裁撤截止日期以前完成业务的平滑迁移和设备退还工作. 历时2个多月,经历了设备梳理.裁撤资源评估.裁撤资源申请.裁撤DB部署.裁撤DB业务关系梳理 ...