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. 数据迁移工具sqoop

    有问题........数据迁移工具sqoop sqoop安装 [root@sqoop data]# wget  wget http://apache.fayea.com/sqoop/1.4.6/sqo ...

  2. mysql用户密码修改,用户添加、删除及设置权限

    一下的示例所用用户名和密码为:test,111111 Mysql密码修改: Mysql修改密码需要root的权限,先执行mysql -uroot -p(密码); 1)使用set password方式来 ...

  3. PRML读书笔记——3 Linear Models for Regression

    Linear Basis Function Models 线性模型的一个关键属性是它是参数的一个线性函数,形式如下: w是参数,x可以是原始的数据,也可以是关于原始数据的一个函数值,这个函数就叫bas ...

  4. 一个DNS统计,RCFs,工具站点

    RCFs http://www.statdns.com/rfc/ DNS resources A collection of DNS related resources DNS Servers Nam ...

  5. Ajax UI方面的处理方式

    一种方式: 从远程直接获取HTML加载到本地 第二种方式从远程获取JSON,到本地使用JS处理数据. var html = "<table cellspacing=\"0\& ...

  6. servlet内置对象

    request                请求对象       类型javax.servlet.ServletRequest   作用域Request response              ...

  7. 关于nodejs能同时接受多少个请求的问题?////zzz

    关于nodejs能同时接受多少个请求的问题? 最近学习node,看了很多教程,都在赞扬nodejs的异步I/O,异步I/O的特点就是,每接收一个请求,使用异步调用处理请求,不用等待结果,可以继续运行其 ...

  8. 用于基于 RPM 的 Linux 平台的 Java

    成为超级用户,方法是运行 su 并输入超级用户口令. 卸载任何之前安装的 Java 程序包. rpm -e package_name 切换到所需的安装目录.键入:cd directory_path_n ...

  9. wampserver 2.5多站点配置

    wampserver2.5版本与之前老版本配置有区别,假设wamp安装在d:/wamp下.第一步:修改D:/wamp/bin/apache/apache2.4.9/conf/httpd.conf 文件 ...

  10. c# winform vlcPlayer播放器

    vlcPlayer是一款免费开源的播放器项目,可以播放几乎所有的视频格式. 第一步:获取dll 安装vlcplayer视频播放器,在安装目录下面获取所需的dll. dll文件夹:plugins 还有2 ...