Educational Codeforces Round 55:A. Vasya and Book
A. Vasya and Book
题目链接:https://codeforc.es/contest/1082/problem/A
题意:
给出n,x,y,d,x是起点,y是终点,d是可以跳的格数,注意是不能跳出边界,最少跳到1,最多跳到n。问最少跳到y的步数是多少。
题解:
从x到y,有三种途径,一是从x直接跳 |y-x|/d 步到y;二是先从x跳到1,然后再跳到y;三是先从x跳到n,然后再跳到y。
注意一下从x跳到边界的步数就好了。
代码如下:
#include <bits/stdc++.h>
using namespace std; long long t,n,x,y,d; int main(){
cin>>t;
while(t--){
cin>>n>>x>>y>>d;
bool flag=false;
long long ans=;
if(abs(y-x)%d==) ans=abs(y-x)/d;
if((y-)%d== ) ans=min(ans,(x-)/d+((x-)%d!=)+(y-)/d);
if((n-y)%d== ) ans=min(ans,(n-x)/d+((n-x)%d!=)+(n-y)/d);
if(ans!=) cout<<ans<<endl;
else cout<<"-1"<<endl;
}
return ;
}
Educational Codeforces Round 55:A. Vasya and Book的更多相关文章
- Educational Codeforces Round 55:B. Vova and Trophies
B. Vova and Trophies 题目链接:https://codeforc.es/contest/1082/problem/B 题意: 给出一个“GS”串,有一次交换两个字母的机会,问最大的 ...
- 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 ...
- Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency
E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition
C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...
- Educational Codeforces Round 55 (Rated for Div. 2) A - Vasya and Book
传送门 https://www.cnblogs.com/violet-acmer/p/10035971.html 题意: 一本书有n页,每次只能翻 d 页,问从x页到y页需要翻动几次? 注意:往前翻最 ...
- A. Vasya and Book ( Codeforces Educational Codeforces Round 55 )
题意:当前在看书的第 x 页,每次可以向前或者向后翻 d 页,这个书一共 n 页,问能否用最小操作翻到第 y 页. 题解:三种情况:1.直接翻能到的一定最短. 2.先翻到第一页,然后往后翻,翻到第 y ...
- Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】
传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...
- Educational Codeforces Round 55 (Rated for Div. 2) Solution
A. Vasya and Book Solved. 三种方式取$Min$ #include <bits/stdc++.h> using namespace std; #define ll ...
随机推荐
- 141. 环形链表 LeetCode报错:runtime error: member access within null pointer of type 'struct ListNode'
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- spoj1026 favorite dice
#include <bits/stdc++.h> using namespace std; int n,t; ; double dp[N]; /* 甩一个n面的骰子,问每一面都被甩到的需要 ...
- java 上溯造型与下塑造型
父类: package com.neusoft.chapter07; public class Father { public int i = 1; public void say(){ System ...
- PowerPC为什么会没落,我自己的反思学习总结
PowerPC为什么会没落? Intel通过酷睿,拉大优势,使得苹果抛弃PowerPC PowerPC当年异常强大,曾经是Xbox的处理器,是Mac的处理器,占据了服务器的半壁江山:当时刚刚接触C ...
- leetcode笔记--3 Niim game
question: You are playing the following Nim Game with your friend: There is a heap of stones on the ...
- 用es6写一个分数库
es6发布后nodejs开始更新.最近写一些库发现新特性还是很好用的,于是回来写一个分数库练手. 对于es6本身 ... => 以及 array.includes 很简洁.class依然不是很顺 ...
- Linux环境搭建系列之sorl服务器的安装部署
http://blog.csdn.net/upxiaofeng/article/details/51425732
- Linux-Shell脚本编程-学习-3-Shell编程-shell脚本基本格式
前面两篇文章基本介绍了一部分linux下的基本命令,后面还需要大家自行了解下linux的文件系统的磁盘管理部分,这里就不在写了. 什么是shell编程,我也解释不来,什么是shell脚本了,我理解就是 ...
- MySQL☞insert value与values
最近公司事情太忙,作为以一挑十的测试,只能苦逼的累死累活的.好不容易临近上线,可以偷个懒写个文章. 简单的说说如何向表中插入数据: 1.向表中所有的列插入数据(插入多行数据): insert int ...
- jquery在页面加载完成后再append的元素事件无效问题
最近遇到一个问题,jquery在页面加载完成后再append的元素,append元素上有onclick事件,但是在append的元素上怎么点击都不会触发onclick事件.就如: <ul cla ...