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 ...
随机推荐
- 第一个.NET小程序
一.用户需求 做一个简单的网页版销售合同签核系统 1.业务员需要在手机或者电脑上操作,Key入销售合同 2.业务员填入相应的合同信息,对应主管签核 3.最终签核完,生成PDF版的销售合同,且上面自动加 ...
- java之hibernate之基于主键的双向一对一关联映射
这篇 基于主键的双向一对一关联映射 1.依然考察人和身份证的一对一关系,如果采用主键关联,那么其表结构为: 2.类结构 Person.java public class Person implemen ...
- SQL递归获取树型路径中文名称
项目中遇到一个树型结构表要根据任意传入节点获取它从根节点一直到自身节点的全部路径的中文名称,并且用'>'与分隔. 我使用在sqlServer中写了一个解析函数方便开发调用. USE [RP_ER ...
- Java序列化流
1.什么是序列化流 序列化就是把Java对象“流化”,序列化后的Java对象可以保存到本地文件系统,或者通过Socket传输到其他的服务器. Java中对象序列化有以下特点: 1)类实现java.io ...
- 3.建造模式(Builder)
注:图片来源于 https://www.cnblogs.com/-saligia-/p/10216752.html 建造模式UML图解析: 代码: Director.h // // Created b ...
- python3基础之“小练习(3)”
(二十四)将字符串"A screaming comes across the sky."中所有的"s"字符替换为美元符号. # a="A scream ...
- 恶意代码分析学习之dll相关记录
kernel32.dll 这是一个很常见的DLL,它包含核心系统功能,如访问和操作内存.文件和硬件,Kernel32.dl等等 Advapi32.dll 这个DLL提供了对核心Windows组件的访问 ...
- svn代码冲突
转自:https://blog.csdn.net/pengweid/article/details/49821117 svn代码提交报以下错误,错误原因: [MenuUCCImpl] 代码冲突 org ...
- 常见SVN图标的含义
转自:https://www.cnblogs.com/genhaosan/articles/5129791.html 灰色向右箭头:本地修改过 蓝色向左箭头:SVN上修改过 灰色向右且中间有个加号的箭 ...
- git 分支查看与切换
git 分支查看与切换 # 1.查看所有分支 > git branch -a # 2.查看当前使用分支(结果列表中前面标*号的表示当前使用分支) > git branch # 3.切换分支 ...