zoukankan      html  css  js  c++  java
  • <Codeforce>1082A. Vasya and Book

    题目描述:

    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 nn, xx, yy, dd (1n,d1091≤n,d≤109, 1x,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.

    INPUT:

    3
    10 4 5 2
    5 1 3 4
    20 4 19 3
    

    OUTPUT:

    4
    -1
    5
    

    记得fmax和fmin要开G++编译器!

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    using namespace std;
    int main(){
    	int t,p,a,b,x;
    	cin>>t;
    	while(t--){
    		long long sum=0;
    		cin>>p>>a>>b>>x;
    		if(a==b){
    			cout<<0<<endl;
    		}else{
    			int k=abs(a-b);
    			
    			if(k%x==0){
    				sum=k/x;
    				cout<<sum<<endl;
    			}else{
    				/*两种情况,a->1->b,a->p->b*/
    				int m1,m2;
    				if((a-1)%x==0){
    					sum+=(a-1)/x;
    				}else{
    					sum+=(a-1)/x+1;
    				}
    				if((b-1)%x==0){
    					sum+=(b-1)/x;
    				}else{
    					sum=-1;
    				}
    				m1=sum;
    				sum=0;
    				if((p-a)%x==0){
    					sum+=(p-a)/x;
    				}else{
    					sum+=(p-a)/x+1;
    				}
    				if((p-b)%x==0){
    					sum+=(p-b)/x;
    				}else{
    					sum=-1;
    				}
    				m2=sum;
    				//cout<<m1<<endl<<m2<<endl<<endl;
    				if(m1!=-1&&m2!=-1){
    					if(m1<m2){
    						cout<<m1<<endl;
    					}else{
    						cout<<m2<<endl; 
    					}
    					//cout<<fmin(m1,m2)<<endl;
    				}if(m1!=-1&&m2==-1){
    					cout<<m1<<endl;
    				}if(m1==-1&&m2!=-1){
    					cout<<m2<<endl;
    				}if(m1==-1&&m2==-1){
    					cout<<-1<<endl;
    				}
    			}
    		}
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    HDU6470 ()矩阵快速幂
    O(1)乘法与快速乘O(log)
    imos 学习笔记五 抓拍 c#
    imos 学习笔记四 录像 c#
    imos 学习笔记三 下载指定时间段视频信息 c#
    imos学习笔记二 用户登录c#
    imos学习笔记一 sdk开发环境
    Hbase(nosql)体系结构有基本操作 笔记八
    zookeeper的安装与配置 笔记七
    mapReduce体系结构和各种算法 笔记六
  • 原文地址:https://www.cnblogs.com/Fylsea/p/10046608.html
Copyright © 2011-2022 走看看