思维水题

题目链接:https://codeforces.com/problemset/problem/1082/A

 

 

题目意思:

一本书有1到n页,起始页数是x,

一次翻页只能翻d(向前或者向后翻,但是不能翻出去,例如n=5,x=1  y=5,d=10     )   这种情况下是可以翻页到5的。

问能否翻y页。否输出-1,能则输出从x到y所需的最少翻页次数

 

 

思路:

1.  x能直接到y。

2.  起点x非1且非n,同时d>=n。

3.  x先到1,再到y。

4.  x先到n,再到y。    

 

AC代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x))
using namespace std;
typedef long long ll;
const ll inf=0xfffffffff;
const double eps=1e-12;
int main()
{int t;cin>>t;while(t--){ll n,x,y,d;cin>>n>>x>>y>>d;if (x==y){printf("0\n");continue;}else if (abs(x-y)%d==0){printf("%d\n",abs(x-y)/d);continue;}else if ((y!=1&&y!=n)&&d>=n){printf("-1\n");continue;}else{        //x->1->yll ans1=0,ans2=0,ans;ans1=ans1+(x-1)/d;if ((x-1)%d!=0){ans1+=1;}if ((y-1)%d!=0)ans1=inf;else ans1=ans1+(y-1)/d;//x->n->1ans2=ans2+(n-x)/d;if ((n-x)%d!=0)ans2++;if((n-y)%d!=0)ans2=inf;elseans2=ans2+(n-y)/d;
//            printf("%lld\n\n",ans2);ans=min(ans1,ans2);if (ans==inf)printf("-1\n");else printf("%lld\n",ans);
//            printf("inf=%lld",inf);
        }} return 0;
}

 

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

Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently displaying the contents of page xx, and Vasya wants to read the page yy. There are two buttons on the book which allow Vasya to scroll dd pages forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of 1010 pages, and d=3d=3, then from the first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page — to the first or to the fifth; from the sixth page — to the third or to the ninth; from the eighth — to the fifth or to the tenth.

Help Vasya to calculate the minimum number of times he needs to press a button to move to page yy.

Input

The first line contains one integer tt (1t1031≤t≤103) — the number of testcases.

Each testcase is denoted by a line containing four integers nnxxyydd (1n,d1091≤n,d≤1091x,yn1≤x,y≤n) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.

Output

Print one line for each test.

If Vasya can move from page xx to page yy, print the minimum number of times he needs to press a button to do it. Otherwise print 1−1.

Example
input
Copy
3
10 4 5 2
5 1 3 4
20 4 19 3
output
Copy
4
-1
5
Note

In the first test case the optimal sequence is: 421354→2→1→3→5.

In the second test case it is possible to get to pages 11 and 55.

In the third test case the optimal sequence is: 47101316194→7→10→13→16→19.

 

转载于:https://www.cnblogs.com/q1204675546/p/10060549.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/352039.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

常用的位运算

1、按位与 & 0 & 0 0&#xff1b;0 & 1 0&#xff1b;1 & 0 0&#xff1b;1& 1 1 同时为1则结果为1&#xff0c;否则为0&#xff1b; 如3 & 8 3 00000011 5 00000101 结果为 00000001 2、按位或 || 0|00; 0|11; 1|01; 1|11; 两…

atom feed_适用于Atom Feed的Spring MVC

atom feed如何仅使用两个类就将提要&#xff08;Atom&#xff09;添加到Web应用程序&#xff1f; Spring MVC呢&#xff1f; 这是我的假设&#xff1a; 您正在使用Spring框架 您有一些要发布在供稿中的实体&#xff0c;例如“新闻” 您的“新闻”实体具有creationDate&#…

argb可以和rgb同步吗_神光同步酷炫幻彩,安钛克光棱120 RGB风扇套装

目前这个时代&#xff0c;电脑的各种配件来说&#xff0c;走向了万物皆要有光的风格。相信发光耳机&#xff0c;发光键盘&#xff0c;发光鼠标大家也都见过不少。侧透机箱甚至全透机箱搭配各类酷炫光效在电竞游戏领域也变得越来越火。许多小伙伴在组装一款电脑时也会选择发光风…

使用GDB命令行调试器调试C/C++程序

编译自&#xff1a;http://xmodulo.com/gdb-command-line-debugger.html 作者&#xff1a; Adrien Brochard 原创&#xff1a;LCTT https://linux.cn/article-4302-1.html 译者&#xff1a; SPccman 本文地址&#xff1a;https://linux.cn/article-4302-1.html 没有调试器的…

制造业物料清单BOM、智能文档阅读、科学文献影响因子、Celebrated Italian mathematician ZepartzatT Gozinto 与 高津托图...

意大利数学家Z.高津托 意大利伟大数学家Sire Zepartzatt Gozinto的生卒年代是一个谜[1]&#xff0c;但是他发明的 “高筋图” 在 制造资源管理、物料清单&#xff08;BOM&#xff09;管理、智能阅读、科学文献影响因子计算 等方面具有重要应用。 高津托图 下图是一个制造业物料…

Dools的DMN运行时示例

正如去年宣布的那样 &#xff0c;Drools 7.0将在合规性级别3上为DMN模型提供完整的运行时支持。 在撰写本文时&#xff0c;运行时实现功能已经完成 &#xff0c;并且团队现在正在努力进行改进&#xff0c;以进行错误修复和用户友好。 不幸的是&#xff0c;对于7.0版本&#x…

走向开放、拥抱开源 —— 如何为代码选择一个合适的开源协议

目录 一. 前言 二. 开源协议选择 2.1. 何为 LICENCE&#xff1f; 2.2. 快速选择开源协议 三. 主流开源许可协议&#xff08;Open Source License&#xff09; 3.1. 常用开源协议 3.2. MIT 协议 3.3. BSD 协议 3.4. Apache Licence 协议 3.5. LGPL 协议 3.6. GPL 四…

MATLAB提取矩阵中的一部分

MATLAB对矩阵的操作十分灵活&#xff0c;下面对最近遇到的进行总结&#xff1a; 格式A(m,n)&#xff0c;用于提取矩阵A中符合m,n要求的部分 1、提取某个元素&#xff0c;则m,n为数字标量&#xff0c;如A&#xff08;2,3&#xff09;为第二行第三列的元素。 2、提取某行某列 A…

garch模型python步骤_GARCH模型的建模步骤?

泻药&#xff0c;我将建立道琼斯工业平均指数(DJIA)日交易量对数比的ARMA-GARCH模型来演示建模步骤。原文链接&#xff1a;R语言&#xff1a; GARCH模型股票交易量的研究道琼斯股票市场指数​tecdat.cn获取数据load(fileDowEnvironment.RData)日交易量每日交易量内发生的 变化。…

JavaScript里面的居民们1-数据

编码 首先练习数字相关的一些操作&#xff1a; <div><label>Number A:<input id"radio-a" type"radio" name"math-obj" value"a"></label><input id"num-a" type"text"> <label…

GCC + pthread

多线程介绍POSIX 1003.1-2001 定义了多线程编程的标准API。这个API就是广为人知的pthreads。它的目的在于为跨平台编写多线程程序提供便利。本文介绍了Linux 和 WIN32 平台下的多线程程序的编写方法Linux 系统对 pthreads 提供了良好的支持。一般地安装完Linux系统后在/usr/inc…

MATLAB的size、length函数

size&#xff08;&#xff09;&#xff1a;获取矩阵的行数和列数 &#xff08;1&#xff09;ssize(A), 返回一个行向量&#xff0c;该行向量的第一个元素是矩阵的行数&#xff0c;第二个元素是矩阵的列数。 &#xff08;2&#xff09;[r,c]size(A), 当有两个输出参数…

python两个时间内的工作日_如何在Python中找到两个日期之间的星期一或任何其他工作日的数目?...

这是高效的-即使在开始和结束之间有一万天的时间-而且仍然非常灵活(它在sum函数内最多迭代7次)&#xff1a;def intervening_weekdays(start, end, inclusiveTrue, weekdays[0, 1, 2, 3, 4]):if isinstance(start, datetime.datetime):start start.date() # make a date from …

JS--继承

构造函数、原型、实例、原型链之间的联系 描述&#xff1a;每个构造函数都有一个原型对象&#xff1b; 每个原型对象都有一个指针&#xff0c;指向构造函数&#xff1b; 每个实例对象都有一个内部指针&#xff0c;指向原型对象&#xff1b; 若此时的原型对象是另一个类型的实例…

Linux C/C++多线程pthread实例

inux中C/C开发多线程程序多遵循POSIX线程接口&#xff08;也就是pthread&#xff09;&#xff0c;pthread涉及函数很多个&#xff08;更多参见pthread.h头文件&#xff09;&#xff0c;常用的有pthread_create、pthread_dispath、pthread_mutex_lock&#xff08;互斥锁定&#…

python 横向合并_使用Python横向合并excel文件的实例

起因&#xff1a;有一批数据需要每个月进行分析&#xff0c;数据存储在excel中&#xff0c;行标题一致&#xff0c;需要横向合并进行分析。数据示意&#xff1a;具有多个代码&#xff1a;# -*- coding: utf-8 -*-"""Created on Sun Nov 12 11:19:03 2017author:…

javafx游戏_JavaFX游戏(四连环)

javafx游戏这是我的第一个JavaFX游戏教程&#xff0c;也是我关于JavaFX面板的第一篇博客文章。 我仅用200几行代码就完成了这四款连接游戏&#xff0c;足以应付一个简单的游戏。 我在这里使用GridPane面板对磁盘进行布局&#xff0c;GridPane是JavaFX布局窗格之一&#xff0c;但…

Matlab的sort函数

1、Matlab自带排序函数sort用法 [Y,I] sort(X,DIM,MODE) sort函数默认Mode为ascend为升序&#xff0c;sort(X,descend)为降序排列。 sort(X)若X是矩阵&#xff0c;默认对X的各列进行升序排列 sort(X,dim) dim1时等效sort(X)dim2时表示对X中的各行元素升序…

django HttpResponse的用法

一、传json字典 def back_json(rquest):#JsonResponse父类是HttpResponse&#xff0c;原码里调用了json.dumps()from django.http import JsonResponseback_msg {name:name,age:123}return JsonResponse(back_msg) 二、传列表 def back_json(rquest):#JsonResponse父类是HttpR…

gcc/g++ 链接库的编译与链接

程序编译一般需要经预处理、编译、汇编和链接几个步骤。在实际应用中&#xff0c;有些公共代码需要反复使用&#xff0c;就把这些代码编译成为“库”文件。在链接步骤中&#xff0c;连接器将从库文件取得所需的代码&#xff0c;复制到生成的可执行文件中&#xff0c;这种库称为…