A. Vasya and Book ( Codeforces Educational Codeforces Round 55 )
题意:当前在看书的第 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 )的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers
题目链接:http://codeforces.com/problemset/problem/616/A 题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小 模拟即可.提供两个版本,数组 ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- centos7.5 离线安装ntp服务
安装 #检查rpm包 rpm -qa | grep ntp #从https://pkgs.org/download/ntp 下载rpm包 ntp-4.2.6p5-28.el7.centos.x86_6 ...
- vs2012配置gitHub管理代码详细步骤
http://www.bitscn.com/pdb/otherdb/201411/411244.html
- top 命令 详解
VIRT:virtual memory usage 虚拟内存 1.进程“需要的”虚拟内存大小,包括进程使用的库.代码.数据等 2.假如进程申请100m的内存,但实际只使用了10m,那么它会增长100m ...
- Flutter Image(图片)
Image是一个用于展示图片的组件.支持 JPEG.PNG.GIF.Animated GIF.WebP.Animated WebP.BMP 和 WBMP 等格式. Image 有许多的静态函数: ne ...
- Elasticsearch 及 Kibana 安装篇
简介 官网-安装介绍 这里记载了各个软件包的安装方法,Linux Mac Windows-- 本文记载的是在 CentOS 系统安装 Elasticsearch 7.0.0 版本的步骤. 安装 Jav ...
- C++之同名覆盖、多态
一.同名覆盖引发的问题 父子间的赋值兼容--子类对象可以当作父类对象使用(兼容性) 1.子类对象可以直接赋值给父类对象 2.子类对象可以直接初始化父类对象 3.父类指针可以指向子类对象 4.父类引用可 ...
- CentOS7.5 部署Ceph luminous
环境 两台CentOS7.5,每台各两块硬盘部署OSD public network = 10.0.0.0/24 cluster network = 172.16.0.0/24 导入ceph的rpm ...
- 【idea】scala&sbt+idea安装配置与测试
一.IDEA安装 下载Community版的IDEA,Ultimate是免费试用版(相当于用到后面要给钱的) ideaIC-2019.2.3.tar.gz 解压IDEA: tar -zxvf idea ...
- javascript_19-DOM初体验
DOM DOM: 文档对象模型(Document Object Model),又称为文档树模型.是一套操作HTML和XML文档的API. DOM可以把HTML和XML描述为一个文档树.树上的每一个分支 ...
- 在LINUX下ORACLE11.2.04的安装脚本及静默安装
首先写好配置环境脚本 cd /opt vi oraclesetup #!/bin/bashecho "back file to filebackup"mkdir -p /opt/f ...