Problem Statement

Snuke has a calculator. It has a display and two buttons.

Initially, the display shows an integer x. Snuke wants to change this value into another integer y, by pressing the following two buttons some number of times in arbitrary order:

  • Button A: When pressed, the value on the display is incremented by 1.
  • Button B: When pressed, the sign of the value on the display is reversed.

Find the minimum number of times Snuke needs to press the buttons to achieve his objective. It can be shown that the objective is always achievable regardless of the values of the integers x and y.

Constraints

  • x and y are integers.
  • |x|,|y|≤109
  • x and y are different.

Input

The input is given from Standard Input in the following format:

x y

Output

Print the minimum number of times Snuke needs to press the buttons to achieve his objective.


Sample Input 1

Copy
10 20

Sample Output 1

Copy
10

Press button A ten times.


Sample Input 2

Copy
10 -10

Sample Output 2

Copy
1

Press button B once.


Sample Input 3

Copy
-10 -20

Sample Output 3

Copy
12

Press the buttons as follows:

  • Press button B once.
  • Press button A ten times.
  • Press button B once.

题意:告诉我们两个数字,两种操作,一种是+1,一种是改变正负,问最小进行几次操作可以将x变成y

解法:讨论讨论讨论。。。。(注意等于0的情况)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll n,m;
cin>>n>>m;
// cout<<min(abs(m-n),min(abs(-1*n-m)+1,min(abs(-1*n+m)+1,min(abs(-1*n+m)+2,abs(-1*n-m)+2))))<<endl;
// if(n==m) continue;
// cout<<n<<" "<<m<<" ";
if(n<m)
{
ll pos=n*(-);
if(n>=&&m>=)
{
//3 5
cout<<m-n<<endl;
}
else if(n<=&&m>=)
{
//-3 5
//3 5
//-3 2
//-3+1=-2 2
if(pos<=m)
{
if(n!=)
{
cout<<m-pos+<<endl;
}
else
{
cout<<m-pos<<endl;
}
}
else
{
// cout<<"A"<<endl;
//ll ans=pos-m;
if(m==)
{
cout<<pos<<endl;
}
else
cout<<pos-m+<<endl;
}
}
else if(n<=&&m<=)
{
//-3 -2
cout<<pos+m<<endl;
}
}
else
{
if(n>=&&m>=)
{
//4 2
if(m!=)
{
cout<<n-m+<<endl;
}
else
{
cout<<n+m+<<endl;
}
}
else if(n>=&&m<=)
{
ll pos=-*m;
if(n==)
{
cout<<-*m+<<endl;
}
else
{
if(n<=pos)
{
cout<<pos-n+<<endl;
}
else
{
cout<<n+m+<<endl;
}
}
}
else
{
if(n==)
{
cout<<(-)*m+<<endl;
}
else
{
cout<<(-)*m-(-)*n+<<endl;
}
}
}
return ;
}

AtCoder Grand Contest 008 A的更多相关文章

  1. AtCoder Grand Contest 008

    AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...

  2. AtCoder Grand Contest 008 D - K-th K

    题目传送门:https://agc008.contest.atcoder.jp/tasks/agc008_d 题目大意: 给你一个长度为\(N\)的序列\(A\),请你构造一个长度为\(N^2\)的序 ...

  3. Atcoder Grand Contest 008 E - Next or Nextnext(乱搞+找性质)

    Atcoder 题面传送门 & 洛谷题面传送门 震惊,我竟然能独立切掉 AGC E 难度的思维题! hb:nb tea 一道 感觉此题就是找性质,找性质,再找性质( 首先看到排列有关的问题,我 ...

  4. AtCoder Grand Contest 008题解

    传送门 \(A\) 分类讨论就行了 然而我竟然有一种讨论不动的感觉 int x,y; inline int min(R int x,R int y){return x<y?x:y;} inlin ...

  5. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  6. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  7. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  8. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

  9. AtCoder Grand Contest 009

    AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...

随机推荐

  1. 【Html 学习笔记】第二节——文本格式

    上一节基本已经了解了一些html的基础,这一节主要学习html处理文本相关内容,直接看内容吧. 字体: 预格式文本:<pre> 地址:<address> 缩写:<abbr ...

  2. JAVA中使用FTPClient上传下载

    Java中使用FTPClient上传下载 在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在c ...

  3. Mongodb基本操作说明

    Mongodb基本操作说明 1.首先cmd(管理员方式运行)下启动mongo服务(类似初始化工具): Mongod.exe 默认文件夹为 :c:\data\db 如果没有创建该文件夹的话,需要先创建该 ...

  4. Quartz2D 编程指南(三)渐变、透明层 、数据管理

    概览 图形上下文 路径 颜色与颜色空间 变换 图案 阴影 渐变 透明层 Quartz 2D 中的数据管理 位图与图像遮罩 CoreGraphics 绘制 Layer 渐变 简介 渐变是从一个颜色到另外 ...

  5. Oracle之nclob类型

    此类型会严重影响查询效率,请少用: nclob字段在查询结果中显示为<NCLOB>,查看nclob类型的值方法有两种 a.可点开...查看具体数据 b.选择所有数据,右击复制到Excel, ...

  6. Resolving SQL Server Disk IO bottlenecks

    网上看到这篇文章挺不错的,直接翻译过来.在尝试诊断SQL Server性能时,不要仅仅依赖某个单一的诊断数据,比如CPU的使用率.SQL Server磁盘性能,就得出结论却忽略的问题的根源.实际上,使 ...

  7. NGINX、PHP-FPM开机自动启动

    NGINX SHELL脚本   放到/etc/init.d/下取名nginx #!/bin/sh # # nginx - this script starts and stops the nginx ...

  8. Hadoop2.x的Eclipse插件编译与安装

    Eclipse的Hadoop插件在开发hadoop应用程序中可以提供一些很方便的操作,可以直接Eclipse中浏览HDFS上的文件,可以直接新建选择MapReduce项目,项目自动包含所有需要的had ...

  9. left join on 和where条件的放置

    先看个例子 以下P1与S1是一对多的关系 结果分别是:1746,1748,1748,1277525,307 由此看出: P1 left join S1 on ... and ...是将S1表中数据先过 ...

  10. [Vuejs] 关于vue-router里面的subRoutes

    刚学习vue,可能有使用不对的地方,希望没有对大家造成困扰! 使用vue-router,为了能够更好的管理.vue文件,需要用到子路由. 先看个错误的例子 routers.js '/company': ...